Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why is this working? a question on generics

 
sangeeta sabharwal
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.util.Collection;
import java.util.ArrayList;
import java.util.Iterator;


public class TestCollection {
public static void main(String args[]) {
Collection collection = new ArrayList<Integer>();
collection.add("ab");

collection.add(2);
collection.add(3);
Iterator<Integer> iterator = collection.iterator();
while(iterator.hasNext())
System.out.print(iterator.next());
System.out.println(collection);

}
}

Why is this allowed ? The program after warning goes on to print
ab23[ab, 2, 3]

 
H Jetly
Ranch Hand
Posts: 41
Google Web Toolkit Redhat Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Collection<Integer> should be the way to go
 
Madhan Sundararajan Devaki
Ranch Hand
Posts: 312
Java MS IE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe the String "ab" is treated as a Hexadecimal Integer!
 
Claudiu Chelemen
Ranch Hand
Posts: 75
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generics are used only by the compiler to provide type safety and eliminate any unwanted casts. However, the compiled code doesn't store the additional information (only to to provide compatibility with legacy code).
In your case, for example, at runtime your code creates an ArrayList of Objects, and not an ArrayList of Integers, how you may have expected.
You may also check that at runtime, regardless of the inferred type, they all have the same class:


Output:
class java.util.ArrayList
class java.util.ArrayList
class java.util.ArrayList

You may also look up "generics type erasure" for additional info.

Cheers!
Claudiu
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sangeeta sabharwal wrote:
Why is this allowed ?

for backward compatibility!
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and Welcome to JavaRanch
 
Federico Cardelle
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sangeeta sabharwal wrote:
Why is this allowed ? The program after warning goes on to print
ab23[ab, 2, 3]

It is allowed by the compiler because you used a raw type, Collection, as the declaration type.
It works at runtime because of type erasure.
 
sangeeta sabharwal
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the type erasure logic....even this should have worked. But this gives an error, as it rightly should.

import java.util.Collection;
import java.util.ArrayList;
import java.util.Iterator;


public class TestCollection {
public static void main(String args[]) {
Collection<Integer> collection = new ArrayList<Integer>();
collection.add("ab");

collection.add(2);
collection.add(3);
Iterator<Integer> iterator = collection.iterator();
while(iterator.hasNext())
System.out.print(iterator.next());
System.out.println(collection);

}
}
 
Claudiu Chelemen
Ranch Hand
Posts: 75
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, this time the compiler sees that you are trying to add a String into a Collection of Integers, thus the compile time error.
The difference is that earlier, you were adding Strings and Integers into a Collection of Objects, which doesn't mind the compiler.

Cheers!
Claudiu
 
sangeeta sabharwal
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. Won't say its absolutely clear .... I think I will need to read and try type erasure in detail to understand it completely. Have some idea for now.


 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic