Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Fullfilling the Object.equals() contract

 
Brian Brumpton
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In trying to work through all the free practice problems I can find, I did the Java SE 7 Programmer I - Sample Questions posted on Oracle's site. The following question, although I answered it correctly, raised some more question in my mind:


What is the result?
A) The output is "true" and MyStuff fulfills the Object.equals() contract.
B) The output is "false" and MyStuff fulfills the Object.equals() contract.
C) The output is "true" and MyStuff does NOT fulfill the Object.equals() contract.
D) The output is "false" and MyStuff does NOT fulfill the Object.equals() contract
E) Compilation fails

Now before I get called on the carpet by Roel or someone else, I did go back to the K&B Study guide to read up on it before posting this.

Chapter 4, page 230 states:

"In terms of understanding the equals() method for the OCA exam, you need to understand two aspects of the equals() method:
  • What equals() means in class Object
  • What equals() means in class String


  • I understand that the equals(), unless overridden, for Objects means they reference the same object and in the String class it can be used to compare String values.

    As for the questions I still have:

    1. I answered the it DOES NOT fulfill the Object.equals() contract. My justification for the answer was that line 10 isn't comparing anything, it was just downcasting the method parameter to a MyStuff object. And line 11 was just returning true so long as the value of String name was not null. Is that justification correct?

    2. Should we, meaning OCA aspirants, need to know more about the equals() contract than the study guide says?
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Brian Brumpton wrote:Now before I get called on the carpet by Roel or someone else, I did go back to the K&B Study guide to read up on it before posting this.

    I wouldn't dare!

    Brian Brumpton wrote:2. Should we, meaning OCA aspirants, need to know more about the equals() contract than the study guide says?

    As this is one of the OCA practice questions from Oracle, I assume you need to know the Object.equals() contract as well. Although I wonder if you really need to know all of this. I can't remember any questions about the equals() contract on my OCA 7 exam. It's mentioned in the javadoc of the equals() method (and on page 581 of K&B7) as well.

    Brian Brumpton wrote:1. I answered the it DOES NOT fulfill the Object.equals() contract. My justification for the answer was that line 10 isn't comparing anything, it was just downcasting the method parameter to a MyStuff object. And line 11 was just returning true so long as the value of String name was not null. Is that justification correct?

    Now you know the contract, you'll probably can figure out yourself why it violates the contract. Otherwise you'll be on my fancy shiny (red) carpet
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Brian Brumpton wrote:2. Should we, meaning OCA aspirants, need to know more about the equals() contract than the study guide says?

    Based on the OCA 7 exam objectives you do not need to know about the equals() contract.
    Java SE 7 Programmer I Exam Topics wrote:Test equality between Strings and other objects using == and equals ()


    In the OCP 7 exam objectives I find this topic:
    Java SE 7 Programmer II Exam Topics wrote:Override the hashCode, equals, and toString methods from the Object class to improve the functionality of your class.
    If you have to know how to override hashCode() & equals() you need to know about the contract of each method, otherwise you end up with a wrong implementation (and this can have huge implications).
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic