Man Suraj
Someone says "Impossible is nothing". The man next him says "Let me see you licking your elbow tip!"
Maan Suraj wrote:
What is not working according to my expectations is below
i thought both if's will be evaluated to true and S.O.P s will be printed.But to my surprise, both if conditions turned out to be false.
Why i thought true?
The reason being ,since all three string references are initiated with value "nice", i thought the concept of string constant pool will come into picture, and therefor t1 ,t2 and t3 will refer to same object on heap.However i see this is not how it works.Can anyone please back it up with some reason
Thanks
OCPJP 6.0
Man Suraj
Someone says "Impossible is nothing". The man next him says "Let me see you licking your elbow tip!"
Joe Okelly wrote: Hi,
Just wanted to add that if you want to compare the contents of the string then you might want to use "equals".
You might want to test the above code using equals and that tests to true in both cases.
Joe.
Chaitanya Kidambi wrote:
Joe Okelly wrote: Hi,
Just wanted to add that if you want to compare the contents of the string then you might want to use "equals".
You might want to test the above code using equals and that tests to true in both cases.
Joe.
hi joe,
equals() is overridden in String class. equals() method with return true only if two references refer to the same object and have same value. so, in your code, first if test will fail as 't1' and 't2' do not refer to the same object. second if test will return true as 't1' and 't3' refer to the same String(which is in the String pool) .
cheers
cheers
Time is what we want the most, but what we use the worst. -- William Penn
Chaitanya Kidambi wrote:Hi,
equals() is overridden in String class. equals() method with return true only if two references refer to the same object and have same value. so, in your code, first if test will fail as 't1' and 't2' do not refer to the same object. second if test will return true as 't1' and 't3' refer to the same String(which is in the String pool) .
equals() method compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.
OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Dennis Deems wrote:
Chaitanya Kidambi wrote:Hi,
equals() is overridden in String class. equals() method with return true only if two references refer to the same object and have same value. so, in your code, first if test will fail as 't1' and 't2' do not refer to the same object. second if test will return true as 't1' and 't3' refer to the same String(which is in the String pool) .
The documentation contradicts this statement:
http://download.oracle.com/javase/1,5.0/docs/api/java/lang/String.html#equals%28java.lang.Object%29
Dennis Deems wrote:Chaitanya, I think you understand it correctly now.
The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).
OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.