Because, String
is an Object
but String
is not StringBuilder
. You can compare reference using ==
on same type and supertype. Look at following example
class A{
}
class B extends A{
}
class C{
}
Now,
A a =new A();
A b =new B(); // or B b =new B();
C c= new C();
System.out.println(a==b); // It is ok
System.out.println(a==c); // It will generate compile time error