File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics question" Watch "Generics question" New topic
Author

Generics question

Aravind Jerubandi
Ranch Hand

Joined: Dec 17, 2006
Posts: 49
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<br />SCJP 5.0 (91%), Preparing for SCBCD.
Aravind Jerubandi
Ranch Hand

Joined: Dec 17, 2006
Posts: 49
Actually i was comparing the above code with the below one:

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38844
    
  23
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

Joined: Dec 17, 2006
Posts: 49
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

Joined: Aug 03, 2008
Posts: 9302
    
  17

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



SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
long meng
Ranch Hand

Joined: Oct 10, 2008
Posts: 58
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


SCJP 5.0 98%<br />SCWCD 5.0 in progress . . .
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

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

Joined: Dec 17, 2006
Posts: 49
Thanks Ankit!!! it makes sense now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics question