File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes String Comparison Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String Comparison" Watch "String Comparison" New topic
Author

String Comparison

Vikram Ramaswamy
Greenhorn

Joined: Mar 21, 2009
Posts: 9
Hi, sorry if this is a lame question that I already ought to know (or has already been answered)
Given Strings s1 and s2, I was wondering what the real difference between s1 == s2 and s1.equals(s2) is


In my understanding, s1 == s2 must fail since they are two separate objects, but when I run the code, both if conditions pass & Hash codes are equal too...
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


In my understanding, s1 == s2 must fail since they are two separate objects,


NO they are 2 references to *same* String literal in String Constant Pool.

check Javaranch Journal.
Vikram Ramaswamy
Greenhorn

Joined: Mar 21, 2009
Posts: 9
I got it now. Thank you.
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


you are Welcome. and Indeed Welcome to Javaranch
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

If you want to do the same check with Integer, Byte or Long - you will see that there are a Constant Pool limit of values in the range -128 to 127

Meaning the == will only work in the range -128 to 127


Regards, Rene Larsen
Dropbox Invite
Muhammad Khojaye
Ranch Hand

Joined: Apr 12, 2009
Posts: 449

Rene Larsen wrote:Meaning the == will only work in the range -128 to 127

integers not being equal to one another


http://muhammadkhojaye.blogspot.com/
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: String Comparison
 
Similar Threads
Technical Interview Questions
meaningfully equivalent variables
please explain the output?
Strings
System.out.Println with operator ==