== vs equals() in java ?

+4 pratt 15 · January 14, 2015
According to my knowledge == tells you whether two references refer to the same object or not  and equals() checks the content inside.
But when I run the below code, it OUTPUT's: one==two (but both refers to the different objects). Why?

public class Class1 {
 public static void main(String[] args) {
       String one = "Hello";
      String two = "Hello";
       if(one==two) {
           System.out.println("one == two");
      else {
         System.out.println("one != two");

Post a Reply


Oldest  Newest  Rating
0 pratt 15 · January 14, 2015
Still it is displaying one==two   :wassat:
0 pratt 15 · January 14, 2015
i m still somewhat confused..... It means initially when two="Hello" (both referring to same object) & when value of two is changed ,they now refer to two different things.  Is dis wat u want to say?
+1 Mike Conroy · January 14, 2015
Hi pratt15 what you are asking is a very common question and when first learning Java it can be quite difficult to get your head round the answer but I will try my best to explain.

In Java we have what are called primitive types and objects. Primitive types can be thought of as "built-in" types a variable can use an example would be int, boolean, double, char, Byte. Whereas objects are a bit more complicated.
When using primitive types we can always use the "==" to check whether they are equal and be sure to get the correct result.
However, when comparing Objects (String is an object) we must use the equals() method as this checks the contents of the object instead - in the case of String it would check that each of the characters are the same.

It is to do with the way they are referenced internally and can be thought of like this - when we declare an Object the variable itself is simply pointing to a location where the object is stored so if we use the "==" on two objects we would be comparing the memory addresses of the two objects. Whereas with primitive types they are stored directly with their value.

public class Example{
   public static void main(String[] args){
       int x = 1; //The number "1" is stored in both these variables
       int y = 1;
       System.out.println(x==y); //Here we are comparing the VALUE of two primitive types.
       String x = "Hello"; //Here the variable x simply holds the address of the String Object we created
       String y = "Hello"; //The variable y holds the address of the new String Objects
       System.out.println(x==y); //This would be comparing the addresses of the two objects
       //Both of the lines below will return true and there is essentially no difference between the two.
       //By using the equals() method we are checking the VALUES of the String Object.

You should note that whilst the above is true for Objects String is a slightly different case due to optimisations which can cause 2 String objects with the same value to point to the same address.

I may not have worded this too clearly as it's late and I'm tired, so will try and clear it up in the morning!
Hope this helps.

Just re-read your question and realised you aren't asking what I initially thought. I see you already understand the difference between == and equals() but wanted to know why == works on Strings.
The reason is due to optimisation where if we declare two Strings with the same value then the JVM will recognise this and make the references point to the same object so we don't store the String twice.
I believe if we did something like this:
String x = "hello";
String y = "test";
//x==y would obviously return false here
y = y.replace("test", "hello");
//I believe here x==y would return false whilst x.equals(y) would return true.

That is my understanding of it anyway!
  • 1

Java / Android Development


Very popular language used to create desktop applications, website applets, and Android apps.

Bucky Roberts Administrator