Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java oracle practice exam

 
Mahtab Alam
Ranch Hand
Posts: 391
1
Java MySQL Database PHP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Answer is :prints false and MyStuff does not fulfills Object.equals() contract

so it means Object.hashCode() contract is included in Object.equals() contract
 
Zhenyi Luo
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mahtab Alam wrote:
public class MyStuff {
MyStuff(String n) { name = n; }
String name;
public static void main(String[] args) {
MyStuff m1 = new MyStuff("guitar");
MyStuff m2 = new MyStuff("tv");
System.out.println(m2.equals(m1));
}
public boolean equals(Object o) {
MyStuff m = (MyStuff) o;
if(m.name != null)
return true;
return false;
}
}


Answer is :prints false and MyStuff does not fulfills Object.equals() contract

so it means Object.hashCode() contract is included in Object.equals() contract


The answer is true, did you miss some info?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This has nothing to do with hashCode. It's just a simple method call.

In line 7, you call equals() on m2, with m1 as the argument. What is called at that point? And how does it get to the result 'true' or 'false'? Trace the method in your head.

If you want to learn about the contract for hashCode(), see the API documentation of the hashCode() method in class java.lang.Object. It explains exactly what the contract is.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic