• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

equals() and == operator in JAVA

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am new to Java. Can anyone please explain me the diff between == and equals() operators in java.
As per my knowledge, the “==” operator is used to compare 2 objects and equals() compares contents of the objects.
I have got a piece of code:
public class EqualsTest {

public static void main(String[] args) {

String s1 = "abc";
String s2 = s1;
String s5 = "abc";
String s3 = new String("abc");
String s4 = new String("abc");
System.out.println("== comparison : " + (s1 == s5));
System.out.println("== comparison : " + (s1 == s2));
System.out.println("Using equals method : " + s1.equals(s2));
System.out.println("== comparison : " + s3 == s4);
System.out.println("Using equals method : " + s3.equals(s4));
}
}

Output:
== comparison : true
== comparison : true
Using equals method : true
false
Using equals method : true

I need to know why System.out.println("== comparison : " + (s1 == s5)); returns true when s1 and s5 are two diff String objects.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sayan Mitra wrote:As per my knowledge, the “==” operator is used to compare 2 objects and equals() compares contents of the objects.


Not quite. '==' compares the references of two objects, and will only every return true if the two references are for the same object.

Basically, you almost never want to use '==' with objects. You can check out the AvoidTheEqualityOperator page for more details.

Winston
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sayan Mitra wrote:I need to know why System.out.println("== comparison : " + (s1 == s5)); returns true when s1 and s5 are two diff String objects.


In this particular case they aren't two different String objects. The JVM is allowed to cache string literals (search for the "String pool" if you want to know more about it), so in that case it uses the same object for s1 and s5. This is safe because Strings are immutable, so there's no danger of changing the object referenced by s1 and accidentally changing s5 as well.

But as long as you are using equals() correctly, as Winston describes, you don't need to worry about whether the Strings are cached or not. That's just an implementation detail.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason that s1==s5 returns true is because of the java "String constant pool". This is a special area of memory for string literals used by java to allow objects to be re-used, thus cutting down on object instantiation.

When you code String s1 = "abc"; java creates an object in the pool. When you code String s5 = "abc", java detects that there is already an object in the pool which has the same value, so instead of creating a new object, it just points the s5 reference to the existing object in the pool. Hence s1 and s5 DO refer to the same object, and true is returned by the == operator.

In contrast, when you code String s3 = new String("abc"), java actually creates a new object on the heap. (I think it also creates an object in the pool, unless a matching literal already exists, as described above, but the reference variable refers to the object on the heap.) So s3 and s4 do point to different objects on the heap, and the == operator returns false.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic