Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

regarding generics

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic