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
Joined: Aug 08, 2007
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
Joined: Mar 26, 2006
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.
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.
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.