aspose file tools
The moose likes Beginning Java and the fly likes String Comparison Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply 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: 341
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/
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: String Comparison
 
Similar Threads
please explain the output?
System.out.Println with operator ==
Strings
Technical Interview Questions
meaningfully equivalent variables