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

Generics question

 
Aravind Jerubandi
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm taking SCJP 5.0 shortly. But really scared of generics and File IO stuff

I was playing with generics and i'm unable to understand why the following works:


Can someone explain why the statement in main method works?

I thought when we create a reference of type 'Test<String>', then all occurrences of 'T' should be replaced by 'String'. But looks like that's not the way it works. There is no constructor that takes Integer and we are creating a reference of type 'Test<String>'. I'm totally confused

[ October 25, 2008: Message edited by: Aravind Jerubandi ]
[ October 25, 2008: Message edited by: Aravind Jerubandi ]
 
Aravind Jerubandi
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually i was comparing the above code with the below one:

 
Campbell Ritchie
Sheriff
Pie
Posts: 47228
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at this version of your programAll I have done is print out the type of "g". You will see that it is no longer type-safe and, although it compiles, there is a warning. I trust you are familiar with the Java� Tutorial?
 
Aravind Jerubandi
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've studied generics, and i know that the type safe are removed through type erasure during compilation. But the way i used to interpret the code was; whenever there is a class type is defined, i used to replace all the occurrences of generic type with the class type. for ex, in the above code:

Test<String> t = new Test(new Integer(1));

As Test class is defined with a generic parameter type 'Test<t>' and in the above line, the reference is defined as 'Test<String>', in my mind, i used to replace all the occurrences of 'T' with 'String' to check for compiler errors. I'm confused here, because the constructor is defined as 'Test(T g)' and in the above code we are passing Integer in the constructor and the parameter type is String.

I know my stupid brain is missing something here and i'm hopelessly confused. Can some one please clarify this with good example.
 
Ankit Garg
Sheriff
Posts: 9495
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well Aravind, since there is no generic declaration in the intialization expression, this is why it is allowing you to use an int in a Object that is typed as String.

new Test(/*can pass anything here*/);

new Test<String>(/*can pass only String here*/);

so as you can see that the intializer in you case is not of any type, so you are able to pass anything to it... You cannot compare it to this code

List<String> l = new ArrayList();
l.add(new Integer(1)); //Compiler error

as here the addition is being made through a type safe l reference. Look at this code

 
long meng
Ranch Hand
Posts: 58
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so,Ankit Garg is that mean in the following code l is not type-safed,because "new ArrayList()" but the add() is the List<String>'s add(),so "l.add(new Integer(1))" won't compile,is it true?

List<String> l = new ArrayList();
l.add(new Integer(1)); //Compiler error
 
Ankit Garg
Sheriff
Posts: 9495
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes long you are right. Once you have assigned an untyped arrayList to a typed one, then all the code after that will use the type of the arrayList. But the code is not necessarily type safe because you have used legacy code. Doing the opposite of your code will lead to a ClassCastException

 
Aravind Jerubandi
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ankit!!! it makes sense now.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic