What if i have two instances of a class named student and both students have the same name, would == return true? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What if i have two instances of a class named student and both students have the same name, would == return true?

29th Mar 2016, 12:16 AM
brendon
2 Answers
+ 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! :)
24th Jun 2016, 1:38 PM
Beardman13
+ 1
Like he said, == checks the reference. To compare the values use the method equals () Like: obj.name ().equals (obj2.name ())
29th Jul 2016, 4:58 AM
Romario Teixeira
Romario Teixeira - avatar