This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
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 Java 8 in Action this week in the Java 8 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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String Pooling Question
 
Similar Threads
hashCode and equals
Object Comparison
Equals and == with Objects Problem
String concatenation
Equals and ==