• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java 2 Programmer Exam Cram book errata

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mr. Brogen, in Chapter 3, test question 10 -- I have a real problem with your answer on that one. You've implicitly cast a Long object to an Object. Everything I've read says that when you do this, you only have access to the methods in the item on the left, the "larger domain". The Object domain implements "equals" as a simple "=="; it checks to see if two objects are "the same", NOT if they have the same contents. Since you didn't state that there was an overridden implementation of 'equals' in the Long object, I have to assume it uses the one in Object. Therefore the statement at line 5 ( if (A.equals(L)) would yield false and your answer is wrong. Please let me know your thinking on this. I hope that the answer does not indicate that I'm supposed to know the implementation of every class in the API by heart, including Long.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

You do need to know something about the "core Java APIs," which certainly includes all the wrapper classes like Byte, Short, Character, Integer, Float, Double,Boolean, and Long, and other fundamental classes like String and StringBuffer. It's enough to know that equals() is implemented sensibly for all of these classes.

You also need to understand -- and from your question, I don't know if you do or not -- polymorphism. A cast might change what the compiler can see, but it never changes what happens at runtime. If Long has its own equals(), then this is used even if the compile-time type of a reference is Object.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Since you didn't state that there was an overridden implementation of 'equals' in the Long object,


Very true, I didn't state that. You should be sufficiently familiar with the classes in java.lang which correspond to the Java primitives to realize that they implement their own equals methods. Since these objects hold primitive values, it would be absurd to have it any other way.
The operation of equals() in the various java.lang core classes frequently comes up in the real world and in the exam.
You should read the JavaDocs for java.lang.Object, paying particular attention to the equals and hashCode methods.

Bill
 
Ken Hanks
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Ernest and Bill, for your answers and information. Sometimes, late at night, I lose common sense. I've since worked out a lot of examples in JBuilder and it's all making a lot more sense now. Thanks again!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic