This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
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.
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.
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.
Joined: Mar 04, 2006
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!