This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

regarding generics

 
Deepthi Kanakam Rajan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic