Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is difference ?

 
Salman Mushtaq
Greenhorn
Posts: 27
Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone !
Hope you all are fine . Today in field of programming i face a very new and basic problem, i declare two Strings
String s1;
String s2;
Now initiate these string with same sentence like
s1 = "Hello";
s2 = "Hello";
now i check
if(s1==s2)
it will return true but when i make an object like
String s1 = new String("Hello");
String s2 = new String("Hello");
if(s1==s2)
it will return false. Here i am little bit confused, what the actually scene behind both cases, and one thing more when i check through ".equal" in both cases it return true, please give me some concept.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The == operator always compares references. If two references point to the same object, or are both null, then == evaluates to true. Otherwise, it evaluates to false.

The equals() method does whatever we tell it to do. It is meant to determine if the states (contents) of two objects are "equal" according to whatever the semantics of equality are for the class in question. For Object, equals() just uses ==, but for other classes, it does things that make sense for that class. For String, equals() looks at the character sequences of the two objects.

So why did == give true in one case and false in the other? If you read my very first sentence above, you already know what the answer must be. It's that in one case the two references pointed to the same object, and in the other case, they didn't.

When we do new Xxx(), that always creates a new object, so obviously the new String() case will have to give us a result of false for ==, since we can see we're creating 2 new objects.

However, for certain objects of certain classes--String literals and small values of primitive wrappers--the objects are cached, and our code sometimes refers to the objects in the cache (also known as a constant pool) instead of creating new objects. When we do String s = "abc"; it doesn't create a new String object, just gives us a reference to the one in the String pool.

Note, however, that you should never rely on this behavior as a shortcut for checking equality. ALWAYS use equals() if you want to know if two references point to objects with equal contents.
 
Winston Gutkowski
Bartender
Pie
Posts: 10086
55
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Salman Mushtaq wrote:Here i am little bit confused, what the actually scene behind both cases, and one thing more when i check through ".equal" in both cases it return true, please give me some concept.

As Jeff says, you should AvoidTheEqualityOperator (←click). I suggest you also read the page about CachedObjects; it may help to explain things for you.

Winston
 
Campbell Ritchie
Sheriff
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please search this forum. You will find many threads about that subject. This post shows the sort of errors which can occur if you use ==.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic