wood burning stoves 2.0*
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 Java 8 in Action this week in the Java 8 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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Two Empty Generic Class Comparison
 
Similar Threads
generics
whizlab question for SCJP1.5
Whizlabs question - Generics/Wrapper classes
generics...
regarding generics