• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

why is this working? a question on generics

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]

 
Ranch Hand
Posts: 41
Google Web Toolkit Tomcat Server Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Collection<Integer> should be the way to go
 
Ranch Hand
Posts: 312
MS IE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe the String "ab" is treated as a Hexadecimal Integer!
 
Ranch Hand
Posts: 75
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sangeeta sabharwal wrote:
Why is this allowed ?


for backward compatibility!
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and Welcome to JavaRanch
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
I'm doing laundry! Look how clean this tiny ad is:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic