Java J2EE Portal
Enterprise Java Station
J2EE curve
Java News / Articles
Java News / Articles
dual signature
Security and Threat Models - Secure Electronic Transaction (SET) Protocol
Oracle OpenWorld
Fusion Middleware, Virtualization and more from Oracle OpenWorld 2007
EJBToJDO
Migrating a J2EE application from EJB to JDO
Processing...
Buy Java, Deals On Software Technology Store
Click here for great deals on computers, laptops, software and books
Test your Java language skills - Java Quiz 3 PDF Print
Written by Content Team   
Jan 31, 2007 at 12:00 AM

*
Answer for question no. 1: Choice C is the correct answer.

The Gen class is a generic class, T is the name of a type parameter to which we can assign any type. Here, we pass Integer as the type argument value for T. Gen i1=new Gen(new Integer(10));

The return type of the get() method is Integer, but we can receive it in an int variable because Integer unboxes into int. So the value received in i is 10. Thus, the output is 10. Hence, choice D is incorrect. There are no compiler errors or exceptions generated here, so choices A and B are incorrect.

For more information - Generics Tutorial

* Answer for question no.2: Choice C is the correct answer.

The code snippet given in choice C is the most appropriate implementation of the hashCode() method that can be inserted at line 15. The variable "area" is an insignificant variable here; its value can be computed from the other two variables - length and width. Also, the fact that "area" is a final variable indicates that its value cannot be changed later. Hence, it should not be included in the computation of the hash code value for the objects of this class. Thus, choices A and B are inappropriate.

The code snippet in choice D returns "long" instead of "int", hence it will not even compile. Obviously, choice D is incorrect. Only code snippet in choice C uses both significant variables and excludes "area", which can be computed from the other two variables. Hence, choice C is the most appropriate implementation. Note that the question asks for the "most appropriate" implementation, and not merely a legal implementation.

While implementing the equals() and hashCode() methods in your class, you should only consider significant variables from that class in these methods. The variable, which can be computed from the other variables of the class, should not be included in the implementation of these methods.

Besides API documentation, you can read more about the equals and hashCode() methods of Object class in Joshua Bloch's book Effective Java: Chapter 3

Related
Test your Java language skills - Java Quiz 1
Test your Java language skills - Java Quiz Two
Get your JSP / Servlet skills certified
A guide to the Sun Certified Java Programmer exam for J2SE 5.0
User Comments

Comment by Niklas on 2007-02-01 04:49:27
As the Gen class in question 1 is written it is not using generics. It will give a compilation error since the class "T" is unknown. You have to specify a formal type parameter like this: 
 
class Gen { .. } 
 
Then when using the class you have to provide this type paremeter 
 
Gen i1 = new Gen(new Integer(10)); 
 
Then it will print out the result "10". :-)

Comment by Niklas on 2007-02-01 04:52:35
The code in my comment looks just like the original post.. all my angle brackets has been eaten by HTML. And I guess that is what has happened in the original post as well. :-) It should look like this: 
 
class Gen<T> { .. } 
 
Gen<Integer> i1 = new Gen<Integer>(new Integer(10));

Comment by Sanjeev on 2008-08-02 04:34:25
This is the right approach.
Your Name / Email Address
Comment
Spam Protection - Please enter the code in the image -

Listen to code




Add This Feed Button

Enter your Email


Java Expert Interviews
DaveCraneAJAXinterview
Ajax technologies aren't particularly new or sexy
Ramesh Loganathan Pramati
Pramati 4.1 and beyond: An interview with Ramesh Loganathan
The future of the Java platform lies in the mobile world
Processing...
Go to top of page  Home |
SiteMap

Copyright 2004 to 2008 Rightrix Solutions. All rights reserved. All product names are trademarks of their respective companies. Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. Rightrix Solutions and IndicThreads.com are independent of Sun Microsystems, Inc.

Views expressed at IndicThreads.com reflect the views of the authors alone, and do not necessarily reflect those of IndicThreads.com. IndicThreads.com and it's authors are not responsible for reader comments and opinions.

Enterprise Java J2EE JEE Portal >> IndicThreads.com