0

Please help me check this string comparison in java, equal method.

EQAUL METHOD JAVA, COMPARING 2 STRINGS. Please is how i did it right?? Because on this platform i see some bunch of codes something like hashcode, something i did not understand! class animal{ String name; animal(String n){ This.name=n; } Public boolean equals(animal obj){ If (name !=obj.name){ return false; } else{ return true; } } } class main{ Public static void main(String args[]){ animal a1 = new animal("ibrahim"); animal b2 = new animal("ibrahim"); System.out.println(a1.equals(a2)); } }

15th Jan 2022, 11:07 PM
Ibrahim Ahmad
Ibrahim Ahmad - avatar
2 Réponses
+ 3
This might help https://www.sololearn.com/learn/Java/2178/?ref=app A few notes: - Class name should be in PascalCase form (by convention) - String comparison is performed by using equals() method, so `this.name != obj.name` is not quite right. - Code indentation helps in debugging, start learning how to indent code. - It's better to attach a code bit's link than to paste a raw text code. At least, syntax highlighting helps to notify us when we wrote something improperly. * How we share our code bit's link https://www.sololearn.com/post/75089/?ref=app
16th Jan 2022, 4:05 AM
Ipang
+ 1
Thank you
17th Jan 2022, 9:53 AM
Ibrahim Ahmad
Ibrahim Ahmad - avatar