wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes String Pooling Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String Pooling Question" Watch "String Pooling Question" New topic
Author

String Pooling Question

Ed Lance
Ranch Hand

Joined: Aug 02, 2000
Posts: 38
Can anyone tell me why the LAST System.out.println in the code below prints out true? It's the equals method in the Object class that gets run and that is the same as a == (memory) comparison. Do the Strings get put into the String pool when they are cast up to an Object??

Thanks in advance.



Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

Can anyone tell me why the LAST System.out.println in the code below prints out true?
The last line prints "true" because both String objects contain the same sequence of characters. It doesn't really have anything to do with pooling in this case, as it's the String's implementation of equals() that's being evaluated.

It's the equals method in the Object class that gets run and that is the same as a == (memory) comparison.
No, it's the equals method in String that gets called ("late binding", polymorphism, and all that rot). See, in order to figure out which instance method to use at runtime, Java looks to the underlying object (a String instance, in this case), rather than the reference type. The String.equals() method overrides the Object's equals() method, and evaluates the string contents for equality.

Hope this helps...
[ August 18, 2005: Message edited by: Steve Morrow ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: String Pooling Question
 
Similar Threads
Object Comparison
Equals and ==
Equals and == with Objects Problem
hashCode and equals
String concatenation