aspose file tools*
The moose likes Java in General and the fly likes equals() and == operator in JAVA Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "equals() and == operator in JAVA" Watch "equals() and == operator in JAVA" New topic
Author

equals() and == operator in JAVA

Sayan Mitra
Greenhorn

Joined: Dec 30, 2012
Posts: 3
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.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4492
    
    8

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.
Douglas Mortimer
Greenhorn

Joined: Mar 30, 2000
Posts: 7
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: equals() and == operator in JAVA