Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
A friendly place for programming greenhorns!
Big Moose Saloon
Register / Login
Programmer Certification (SCJP/OCPJP)
Deepthi Kanakam Rajan
Joined: May 15, 2006
Oct 22, 2006 22:26:00
This is one of the questions given in epracticelabs demo exam:
public class PayRoll
public static void main(
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
In that case, the answer should have been true true true true.
Appreciate a clarification on this.
Thanks in advance.
Joined: Apr 18, 2006
Oct 23, 2006 01:30:00
Yes, I think you're right. The answer should be true true true true.
Vector<String> employeeList = new Vector<String>(); //at compile time Vector employeeList = new Vector(); //at runtime
Each call to employeeList.getClass(),employeeIdList.getClass(),
employeeProfileList.getClass() returns class
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:
ArrayList list = new ArrayList(); Vector<String>employeeList = new Vector<String>(); System.out.println(employeeList.equals(list)); //true
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
Explain the strange behavior of Boolean Class??
Linked List equals method
Two Empty Generic Class Comparison
From mock exam about Autoboxing
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter
| Powered by
Copyright © 1998-2015