Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Highlighted
Developer
Posts: 777
Registered: ‎07-21-2012
My Device: 9810
My Carrier: 0
Accepted Solution

Need some help me to understand string concept

hi

i am asking simple question , i also googled it but not able to understand clearly.problem is 

1) String is immutable , cannot be changed but my question is why it is immutable?

 

String A="test";

 

A="ghar";

from this example we change string , why it is called immutable ?

 

2) question how to find reference in string , i know == check the reference and equal check the contents.

 

in my example why it print Equal 1 in first case how complier find reference .if str1==str2 reference is equal ?

 

 

String str1="Hello";
String str2="Hello";
String str3=new String("Hello");
System.out.println(str1);
System.out.println(str2.hashCode());
System.out.println(str3.hashCode());

if(str1 == str2)
System.out.println("Equal 1");
else
System.out.println("Not Equal 1");

if(str1 == str3)
System.out.println("Equal 2");
else
System.out.println("I am constructed using constructor, hence not interned");

if( str1.equals(str3) )
System.out.println("Equal 3");
else
System.out.println("Not Equal 3");
}

 

 

 

 

Developer
Posts: 16,999
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Need some help me to understand string concept

if you use A="ghar" you create a new object, you do not modify the previous object.

str1==str2 will never succeed as the '==' operator compares the reference to the object.
equals compares the object itself, so str1.equals(str2) is true, as the equals method of string checks if both contain the same characters.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 777
Registered: ‎07-21-2012
My Device: 9810
My Carrier: 0

Re: Need some help me to understand string concept

 

String abc ="test";

abc ="ghar" is it create the new object ? am i right.?

 

what is the meaning of this?

str1==str2 will never succeed as the '==' operator compares the reference to the object.

 

actually i read this == operator compare the reference not the content..

 

 

Developer
Posts: 16,999
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Need some help me to understand string concept

== checks if both objects are exactly the same (identity)
see
http://www.javapractices.com/topic/TopicAction.do?Id=18
or a multitude of other pages.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 777
Registered: ‎07-21-2012
My Device: 9810
My Carrier: 0

Re: Need some help me to understand string concept

String str1 ="abc";

 

String str2 ="abc";

 

 

if(str1 == str2)
System.out.println("Equal 1");

 

why we don't prefer this method?

 if we are not prefer this..?

why it is printing  Equal 1

Developer
Posts: 777
Registered: ‎07-21-2012
My Device: 9810
My Carrier: 0

Re: Need some help me to understand string concept

what is the meaning
? sorry i am asking simple question again and again

compares only the identity of the objects, using their address in memory (which is usually not desired).
Developer
Posts: 16,999
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Need some help me to understand string concept

because this is not the way java is implemented.

the VM can store one string object and use it for multiple uses, so in this case str1 and str2 actually reference the same object (internally).
but you cannot rely on that.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 777
Registered: ‎07-21-2012
My Device: 9810
My Carrier: 0

Re: Need some help me to understand string concept

String str1 ="abc";

String str2 ="abc";


if(str1 == str2)
System.out.println("Equal");

why it print Equal
Developer
Posts: 777
Registered: ‎07-21-2012
My Device: 9810
My Carrier: 0

Re: Need some help me to understand string concept

String str1 ="ab";

String str2 ="abc";


if(str1 == str2)
System.out.println("Equal");

 

This time also the VM can store one string object. so in this case str1 and str2 actually reference the same object (internally).

so this time also it should print Equal but it does not

Developer
Posts: 16,999
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Need some help me to understand string concept

"ab" and "abc" cannot be stored as the same object internally.

in java you use equals to compare strings. by definition.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter