This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
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 REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "regarding generics" Watch "regarding generics" New topic

regarding generics

Deepthi Kanakam Rajan

Joined: May 15, 2006
Posts: 13

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>();



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

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:
subject: regarding generics
It's not a secret anymore!