Hi! Every body , can anybody please explain the reason behind the following behaviour in case of String literals
String s1="Hello" if(s1=="He"+"llo") System.out.println("equals"); else System.out.println("not equals"); Prints TRUE
String s1="Hello" String s2="He"; String s3="llo" if(s1=s2+s3) System.out.println("equals"); else System.out.println("not equals"); Prints not equals
Please clarify . Thanks in advance
Jolly
Anoobkumar Padmanabhan
Ranch Hand
Joined: Aug 08, 2007
Posts: 103
posted
0
Hi
Are you familiar with teh difference between "==" and .equals method and the String pool concept?
In you example, the operation is ==. ie, you just comparing the references. since you has given String s1="Hello", it will take a String "Hello" from the String pool and returns the refence to that String only.
In the first comaprison, you comapre it with another String in pool, "He"+"llo", which will give the same String object, and returns a true.
In the second case, you compare the SAString with the resultant object of a String manipulation,+. it will conacatenate a String object and return another object, that is newly created, not simply returns a refernce from pool. so th operation will return false.
For more clarrification, please do the function with String s1=new String("Hello"); Then both the comparisons will return false.
Or try with .equals, other than ==. then the comaprisons will return true. [ September 22, 2008: Message edited by: anoobkumar padmanabhan ]
Thanks<br /> <br />Anoobkumar<br />SCJP 1.5
Jolly Tiwari
Ranch Hand
Joined: Mar 26, 2006
Posts: 77
posted
0
Hi! Anoob,
Thanks for the reply. Actually i am pretty comfortable with == and equals().
But the thing which needs clarification is in the behaviour of == in String literals.
if(s1=="He"+"llo") System.out.println("equals");
As the resulting string after concatenation is the same as already present in the literal pool .so the resulting string starts referring to the same s1 String.
But why is this not applicable to the next code snippet
the statement, s1==s2+s3, will operate on two string object and create a new String object. it wont simply returns the refernce of the same string from the pool. thats y the comparison fails in this case.
This is what happens, When you perform "he" + "llo", a new String literal is created and stored in the String Pool and not on the heap.
so when you perform s1=="he"+"llo" it returns true.
In the second case,
s2+s3 creates a new object on the Heap.
and == operator checks for References and not the value. So the second if prints not equal.
If you want to check whether the String Objects are meaningfully equal then use equals() method. The equals method has been overloaded in String class such that
is it true that replace() will create a new String object? But line 1&2 makes s1&s refer to that object so in that case wont s==s1 be true..
kindly help!!
[ September 22, 2008: Message edited by: vidhya suvarna ]
String.replace(char,char) returns a new String reference. the original String stays the same as String is immutable. Hence you get 'false' on comparing.
Originally posted by Jolly tiw: Hi! Every body , can anybody please explain the reason behind the following behaviour in case of String literals
Please clarify .
First of all, keep in mind that you should not depend on this -- it is *not* in the specification whether the compiler should (specifically should not) place something into the pool.
Anyway, in the first case, the compiler is able to calculate the resultant string during compile time, and hence, treat the assignment as a straight assignment to a string literal. (with the result)
In the second case, the compiler is not able to do so, because the strings are not compile time constants. Hence, the string must be calculated during runtime, which doesn't intern the string (by default).
BTW, for the second case, if the two strings were final, then the compiler is able to calculate the resultant string, as it is a compile time constant, and you will get "equals".
As in you code, String s="String".replace('t','T'); //1 String s1="String".replace('t','T');//2
Keep in mind that a string operation will result ina String object, not a simple literal. this is the answer for your question. ie, here you will get two String objects, refernced by s and s1. so the operation, s==s1 will certainly result in false.
vidhya suvarna
Ranch Hand
Joined: Aug 28, 2008
Posts: 148
posted
0
Thanks anoob that does answer my query.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: Behaviour of + in case of String literals