What is the difference between object and object reference?And what do the the following two statements signify?
Statement1::
String s="Bagmita";
Statement2::
String s=new String("Bagmita");
The difference between these two statements can be infered from
this thread.
In the first statement s holds a string literal value. s refers to literal "Bagmita".
In the second statement, s holds a reference to an object that has just been created in memory.
String s1="Bagmita";
String s2=new String("Bagmita");
s1 == s2 compares the memory addresses (object references) and so returns false. But s1.equals(s2) compares what these 2 are refering to (references) and so returns true.