I was reading about == operator in java and found that it used to compare memory reference the below example is from given link.
String obj1 = new String("xyz"); // now obj2 and obj1 reference the same place in memory String obj2 = obj1; if(obj1 == obj2) System.out.printlln("obj1==obj2 is TRUE"); else System.out.println("obj1==obj2 is FALSE");
Note in the code above that obj2 and obj1 both reference the same place in memory because of this line: “String obj2 = obj1;”. And because the “==” compares the memory reference for each object, it will return true. And, the output of the code above will be:
After that I write code randomly to check == operator but why it returning true in this example?
String obj1 = "ABC";
String obj2 = "ABC";
if(obj1 == obj2)
System.out.println("obj1==obj2 is TRUE");
else
System.out.println("obj1==obj2 is FALSE");
Does "ABC" string saved in the one memory place then obj1 and obj2 sharing that memory reference?
Even int also returning true.
int obj1=3;
int obj2=3;