wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes regarding generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "regarding generics" Watch "regarding generics" New topic
Author

regarding generics

Deepthi Kanakam Rajan
Greenhorn

Joined: May 15, 2006
Posts: 13
Hi,

This is one of the questions given in epracticelabs demo exam:

import java.util.*;

public class PayRoll
{

public static void main(String argv[])
{
Vector<String>employeeList = new Vector<String>();

Vector<Integer>employeeIdList = new Vector<Integer>();

Vector<Object>employeeProfileList = new Vector<Object>();

System.out.println(employeeList.getClass()==employeeIdList.getClass());
System.out.println(employeeList.equals(employeeProfileList);
System.out.println(employeeList.getClass()==employeeProfileList.getClass());
System.out.println(employeeIdList.equals(employeeProfileList);

}
}

the answer gven is false false false true.

teh reason given that all instances of a generic class have the same runtime class so getClass() returns java.util.Vector.

In that case, the answer should have been true true true true.

Appreciate a clarification on this.

Thanks in advance.
Mateusz Kwasniewski
Greenhorn

Joined: Apr 18, 2006
Posts: 15
Yes, I think you're right. The answer should be true true true true.



Each call to employeeList.getClass(),employeeIdList.getClass(),
employeeProfileList.getClass() returns class java.util.Vector so both
comparisions with == return true.

On the other hand, to find out why equals returns true in both cases you
should remember that List interface overrides equals. API says:
...two Lists are defined to be equal if they contain the same elements in the same order.

Since in our example the vectors are empty we get true twice.

Even if you have someting like:



You will get true.

It's my first post here, so please correct me if I'm wrong


SCJP5 - 90%<br />SCWCD - 91%
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: regarding generics
 
Similar Threads
Linked List equals method
Explain the strange behavior of Boolean Class??
Two Empty Generic Class Comparison
From mock exam about Autoboxing
wrapper classes