+ 1
What if i have two instances of a class named student and both students have the same name, would == return true?
2 Réponses
+ 3
== checks :
-> if two primitive values (like int, boolean, double,float)
-> if two references are pointing to the same object.
Suppose :
Student s1 = new Student ("James");
Student s2 = new Student ("James");
System.out.print (s1==s2); // would return false as it would check s1 and s2 are pointing to the same object or not. I.e, if the address values associated with s1 and s2 are the same or not.
Hope your doubt is cleared! :)
+ 1
Like he said, == checks the reference. To compare the values use the method equals ()
Like:
obj.name ().equals (obj2.name ())