File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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: 261



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 , Oracle Java Web Component Developer
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: 14156
    
  19

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