0

Is c point to a or is c point to the same memory that a is pointing to? Why does the following code print "We are equal"?

Thanks in advance

    public static void main(String args[]) {
        String a = "10";
        String b = "10";
        String c = a;
        
        if(c.equals(b)) {
            System.out.println("We are equal ");
        }
        else {
            System.out.println("Not equal! ");
        }       
    }
Nowhere Man
  • 19,170
  • 9
  • 17
  • 42
zara
  • 99
  • 1
  • 8
  • Same issue with this code, why isn't ab equal to abc? String a=”meow”; String ab=a+”deal”; String abc=”meowdeal”; System.out.println(ab==abc); – zara Jul 13 '20 at 19:48

1 Answers1

1

Java stores objects by reference... the reference just happens to be the value when we use primitives.

int is a primitive so

a == c => true

a == b => true

b == c => true

With Strings the situation is slightly different. Strings are effectively char arrays: char[] but are not considered primitive types in Java.. so:

"str1" == "str1" => false since you're comparing two object references now.

For this reason the Object class has the equals() method which allows you to compare reference objects (non-primitives) via something other than the object reference ID. The String (a subclass of Object.class) class overrides the equals method.

In your example (above) you say:

So why is ab==abc false. They are both pointing to same address.

This is incorrect. ab points to String that is "meowdeal" and abc points to a String that is also "meowdeal"... but crucially they are two distinct instances. Therefore:

ab==abc => false - here your checking for reference equality - are they the same object reference (no they are not)

ab.equals(abc) => true - here you're checking for string equality (is ab's "meowdeal" the same as abc's "meowdeal" - yes it is.

This is one of those curveball interview questions you might get from time to time so worth being aware of.

Rob Evans
  • 2,822
  • 1
  • 9
  • 15