File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Two Empty Generic Class Comparison

 
Punit Singh
Ranch Hand
Posts: 952
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic