aspose file tools*
The moose likes Java in General and the fly likes why is this working? a question on generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "why is this working? a question on generics" Watch "why is this working? a question on generics" New topic
Author

why is this working? a question on generics

sangeeta sabharwal
Greenhorn

Joined: Jul 25, 2011
Posts: 6
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

Joined: Aug 26, 2010
Posts: 41

Collection<Integer> should be the way to go


Harsh Jetly
Madhan Sundararajan Devaki
Ranch Hand

Joined: Mar 18, 2011
Posts: 312

Maybe the String "ab" is treated as a Hexadecimal Integer!


S.D. MADHAN
Not many get the right opportunity !
Claudiu Chelemen
Ranch Hand

Joined: Mar 25, 2011
Posts: 71

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

Joined: Jan 28, 2008
Posts: 5575

sangeeta sabharwal wrote:
Why is this allowed ?

for backward compatibility!
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

and Welcome to JavaRanch
Federico Cardelle
Greenhorn

Joined: Jul 26, 2011
Posts: 26
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

Joined: Jul 25, 2011
Posts: 6
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

Joined: Mar 25, 2011
Posts: 71

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

Joined: Jul 25, 2011
Posts: 6
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.


 
Consider Paul's rocket mass heater.
 
subject: why is this working? a question on generics