aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes what is the type of  generic variable 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 "what is the type of  generic variable" Watch "what is the type of  generic variable" New topic
Author

what is the type of generic variable

kumarth ravi
Ranch Hand

Joined: Jun 01, 2007
Posts: 48
I)What is the type of 'a' and 'b' variables.

1.List<? super Integer> a=new ArrayList<Integer>();

2.List<? extends Number> b=new ArrayList<Number>();

II)I am giving a class ,How the compiler and jvm will translate at the time of compiling and run time.

2).why type parameters instace creatinon is not allowed.


In my opionin,T is replaced with like this at

java.lang.Number o=new java.lang.Number();

then what is the problem?
Olivier Croisier
Greenhorn

Joined: Mar 14, 2006
Posts: 29
You cannot be sure that the real type (T) will have a no-arg constructor.
So you cannot write T t = new T() !


SCJP 5.0 (100%)<br />SCWCD (getting ready...)
Leandro Melo
Ranch Hand

Joined: Mar 27, 2004
Posts: 401
Hi all.

Originally posted by Olivier Croisier:
You cannot be sure that the real type (T) will have a no-arg constructor.
So you cannot write T t = new T() !


Actually, this is not the real reason why you cannot write new T(). The problem is that generics in Java is supported by a technique called type erasure. With this technique, the generic types only exist at compile time. So, the type T is not really replaced by the real type during runtime. That's why you cannot write new T().

One thing you could do to help clear out things is to compile the following code and disamssemble it (using javap -c). Note that the disassembled code is the same.


[ July 02, 2007: Message edited by: Leandro Melo ]

Leandro Melo
SCJP 1.4, SCWCD 1.4
kumarth ravi
Ranch Hand

Joined: Jun 01, 2007
Posts: 48
Thank you for reply.

other doubts:
-------------

1. if(item instanceof E){ // why compiler error
}

2. E[] iarray=new E[10];

3. E obj=(E) new Object();

why the above statements are not allowed
kumarth ravi
Ranch Hand

Joined: Jun 01, 2007
Posts: 48
thank you for reply.


I have executed the code with javap -c. Both Box.java and Box2.java looks
similar.Here T is replaced with java.lang.Object. That is ok.

but in this set mehtod:
-----------------------
"set(T o)" is replaced as a "set(java.lang.Object o)".like that
why cant it replace this statment like this.


T o=new T();
java.lang.Object o=new java.lang.Object();
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Originally posted by kumarth ravi:
why cant it replace this statment like this.
T o=new T();
java.lang.Object o=new java.lang.Object();

Ask yourself if the following code would be correct if the compiler would do your suggested replacement:
kumarth ravi
Ranch Hand

Joined: Jun 01, 2007
Posts: 48
we can't sure that the type parameter which was passed have a that type of constructor or not, that I got it.

thank you.
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Originally posted by kumarth ravi:
we can't sure that the type parameter which was passed have a that type of constructor or not
It has nothing to do with the constructor. Here is the same code without generics and your suggested replacement.

Do you think the above code would work?
kumarth ravi
Ranch Hand

Joined: Jun 01, 2007
Posts: 48
yes,it will work.
can you clear my confusion?
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Originally posted by kumarth ravi:
yes,it will work.
can you clear my confusion?

Have you compiled and run the program?
kumarth ravi
Ranch Hand

Joined: Jun 01, 2007
Posts: 48
It was compiled but thrown a Class cast exception.
Leandro Melo
Ranch Hand

Joined: Mar 27, 2004
Posts: 401
Hi Kumarth.

Originally posted by kumarth ravi:
It was compiled but thrown a Class cast exception.


That's what Manfred was trying to tell you. Replacing T o=new T() by java.lang.Object o=new java.lang.Object() might not be a good idea. You have just seen a situation where it wouldn't work . You might want to take a look at the tutorial I pointed in my first post, it could help you for a better understanding (if you're still confused).
kumarth ravi
Ranch Hand

Joined: Jun 01, 2007
Posts: 48
I got the idea.I will read the material.


Thanks to all for this help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what is the type of generic variable