File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Two Empty Generic Class Comparison Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Two Empty Generic Class Comparison" Watch "Two Empty Generic Class Comparison" New topic
Author

Two Empty Generic Class Comparison

Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
All instances of the generic class have the same runtime class, regardess of actual type parameters.
for instance:

Vector<String> vector=new Vector<String>();
Vector<Integer> vector1=new Vector<Integer>();
System.out.println(vector.getClass()==vector1.getClass());
it will return true.


Empty vector classes are meaningfully equal.

System.out.println(vector.equals(vector1));
it will return true.


if vectors are not empty ...
vector.add("hi");
vector1.add(1);
System.out.println(vector.equals(vector1));
then it will return false.


ArrayList and Vector are the type of List. If two List are empty then they are equal.

ArrayList<Integer> arrayList=new ArrayList<Integer>();
Vector<String> vector=new Vector<String>();
System.out.println(vector.equals(arrayList));
it will return true.


but the concrete class of ArrayList and Vector not same. so..
System.out.println(vector.getClass()==arrayList.getClass());
it will return false.


The same concept will apply to Set classes and Map classes. Remember if generic classe objects are empty then only they are equal.

[ December 08, 2008: Message edited by: Punit Singh ]
[ December 08, 2008: Message edited by: Punit Singh ]

SCJP 6
 
wood burning stoves
 
subject: Two Empty Generic Class Comparison