Reason is when we concatenate Strings using a return from a method, it creates a new String in the memory. But saying,
final String dog = "length: " + "10" would return true when checked for pig == dog. [ November 10, 2006: Message edited by: Jothi Shankar Kumar Sankararaj ]
Reason for the above one to be false...the two objects have different references and they are two different objects. Also the line System.out.println("Animals are equal: " + pig == dog); is seen by the compiler as System.out.println(("Animals are equal: " + pig) == dog); which will anyways return false. Now to prove my first statement "the two objects have different references and they are two different objects" try System.out.println("Animals are equal: " + (pig == dog)); which would still return false. Hope it is clear....
The below one prints true
I think I dont have to give reason here as it is blatant that the code above returns true. You will disagree?? [ November 10, 2006: Message edited by: Jothi Shankar Kumar Sankararaj ]
Reason for the above one to be false...the two objects have different references and they are two different objects. Also the line System.out.println("Animals are equal: " + pig == dog); is seen by the compiler as System.out.println(("Animals are equal: " + pig) == dog); which will anyways return false. Now to prove my first statement "the two objects have different references and they are two different objects" try System.out.println("Animals are equal: " + (pig == dog)); which would still return false. Hope it is clear....
Operator precedence comes to play with the System.out.println("Animals are equal: " + pig == dog); option.
Satisfied sharma Ji....or still you wanna argue???
Hi have you recently applied for Bartender/Sheriff or TrailBoss at JavaRanch. And they (Moderators) have given you assignments to try out some of their activities.
Or you are just Enjoying your friday.
[ November 10, 2006: Message edited by: Harshad Khasnis ]
Originally posted by Harshad Khasnis: Dear Sharma Ji.
Hi have you recently applied for Bartender/Sheriff or TrailBoss at JavaRanch. And they (Moderators) have given you assignments to try out some of their activities.
Originally posted by Harshad Khasnis: Dear Sharma Ji.
Hi have you recently applied for Bartender/Sheriff or TrailBoss at JavaRanch. And they (Moderators) have given you assignments to try out some of their activities.
Thanks, I got the point how we gets the false when we concatenate the strings with a return string from a method and how to take care for printing the stmts in SOPs..
The both references will point two different objects in the string constant pool.The reason being for that is whenever we concatenate the string with a method returned string(dog1),it creates a new object rather than pointing to a same named string constant(dog2) in the pool.
so when we apply reference check equality on both means (dog1==dog2),it returns false.Now change the pig.length() to "10",it must print true.
Hi John, Yes you are right. It does print different values and here's is the reason why. First some stuff to remember: Java uses something called as the string pool to manage string objects ( you dont need to know the details abt the string pool for the SCJP. but you should know that there is something calledas the string pool). Whatever string objects are created are stoed in the string pool so that the string can be reused For ex suppose you say String str="Hello". The first time the program runs the str object is placed in the string pool Now next time say you create another object called str1 as follows: String str1="Hello"; What happens at this point is that the Java compiler knows that the string object with the value "Hello" is already present in the string pool. So instead of creating a new String object what the compiler does to optimize memory usage is to make str1 also point to the same string object as str.
Hence the equality test for the string objects str and str1 i.e (str==str1) returns true as the two references are the same.
Now when a method returns a String object it creates a new String object hence if we were to declare another string object say str2 as follows String str2=getString(); where let's assume that getString returns "Hello"; here when the method returns a string a new string object is created in the string pool and the reference variale str2 points to that Hence the test str==str2 fails as the two objects refer to different objects on the pool albeit the objects they are referring to have the same value "Hello". Hope this explaination solves your confusion
Hi John, The difference in the following two statement is nothing but the operator precedence. 1) System.out.println("Animals are equal: " + dog == pig); 2) System.out.println("Animals are equal: " + (dog == pig));
On the 1) Statement + operator take the precedence and comparison will be between ("Animals are equal: " + dog) and (pig) whereas on the statement 2) the comparison is between (dog) and (pig). Hope this will clarify your doubt.