The difference between == and equals() when used in java.lang.String for Java

For students who would usually be taught C as their first programming language, a shift to Java could quickly make a confusion as they are crossing the boundaries between procedural and Object Oriented Programming. Both operators can be used in Java, in fact the second one is not called an operator but a method.

 

==

This is called the "Equal to" operator and is used to compare "primitive" types or check if you have equal object references(or do they refer to the same object in the heap). There are 8 primitive types in Java and you can usually identify them

byte, short, int, long, float, double, boolean, char

 

When used with Strings, one might assume that  you are making a comparison with the String values, but no. We mentioned above that "Equal to" operator checks for object references when used with objects.

 

So let's assume the following code:

 

1
2
3
4
5
6
        String s1 = "STRING ME";
        String s2 = "STRING ME";

        System.out.println("s1: " + s1);
        System.out.println("s2: " + s2);
        System.out.println("s1 == s2 is " + (s1 == s2));

This would output:

s1: STRING ME
s2: STRING ME
s1 == s2 is true

 

Why? The JVM does some optimization step with Strings(i.e the Strings get "pooled"). Basically, the JVM makes you point to the same Object reference that is pooled in the heap.

 

Let's make some modification with the Strings

 

1
2
3
4
5
6
        s1 += "2";
        s2 += "2";

        System.out.println("s1: " + s1);
        System.out.println("s2: " + s2);
        System.out.println("s1 == s2 is " + (s1 == s2));

 

The output is now:

s1: STRING ME2
s2: STRING ME2
s1 == s2 is false

 

Why? Because Strings are "immutable", new Object references are actually created. The String was not actually modified, it is a new String object. They are now different object references.

 

Now, what most of us usually want is "OBJECT EQUALITY", and that's what the next thing is for

 

equals()

All objects can use and override the equals() method. Any instance of a class you use or created automatically inherits this method. Without overriding, you use the default implementation of the Object class which is not very helpful. This works differently when used with the java.lang.String class as it does some character comparisons. You cannot override this method in String because String is a final class and cannot be extended.

 

Using a similar set of code, let's try to make modifications with the Equal To operator, for the String references to use the equals() method.

1
2
3
4
5
6
7
8
9
10
11
12
13
        String s1 = "STRING ME";
        String s2 = "STRING ME";

        System.out.println("s1: " + s1);
        System.out.println("s2: " + s2);
        System.out.println("s1 == s2 is " + (s1.equals(s2) ) );

        s1 += "2";
        s2 += "2";

        System.out.println("s1: " + s1);
        System.out.println("s2: " + s2);
        System.out.println("s1 == s2 is " + (s1.equals(s2) ) );

 

This will now both evaluate to true.

 

Again, when using Strings in Java, think of you're after equal object reference or object equality.

 

 

References:

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/opsummary.html

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.html

Published 04-15-2010 8:01 AM by lamia

Comments

Saturday, May 01, 2010 2:14 AM by Candler

# re: The difference between == and equals() when used in java.lang.String for Java

this is nice information for our project and programs that is java platform. The Double class wraps a value of the primitive type double in an object. An object of type Double contains a single field whose type is double. thnks fr this