This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Java oracle practice exam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Java oracle practice exam" Watch "Java oracle practice exam" New topic
Author

Java oracle practice exam

Mahtab Alam
Ranch Hand

Joined: Mar 28, 2012
Posts: 222



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

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


Oracle Java Programmer , Oracle SQL Expert
Zhenyi Luo
Ranch Hand

Joined: Sep 03, 2012
Posts: 33
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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java oracle practice exam
 
Similar Threads
How is order working in a Collection?
Sorting with Comparable
equals()
.equals()-Regarding collections
Problem with using variable before it is defined