Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

what is the type of generic variable

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You cannot be sure that the real type (T) will have a no-arg constructor.
So you cannot write T t = new T() !
 
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
kumarth ravi
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes,it will work.
can you clear my confusion?
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It was compiled but thrown a Class cast exception.
 
Leandro Melo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got the idea.I will read the material.


Thanks to all for this help.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic