• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Generics doubts

 
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have some questions on generics
1. if List(?) is a reifiable then why List(? extends Object) is not a reifiable type ?? (even though Type Erasure will convert both into List(Object)
according to me reifiable is the type which is completely represented at runtime i.e. no useful information is removed(type information)

2. what is the difference between these two generics method

and

one difference is that first one is the class method, i.e no class instance is needed to call that method. Is there any other difference in the working of these method ?

3. why static generic method cannot use Type parameter defined by class

my reason for this: since static method can be called even if there is no instance and if there is no instance of generic class, the type parameter are unknown... so they are not allowed

4. why Type parameter cant be static

my reason :a) since static variable are the class variable and get there value at compile time, but Type parameter are unknown at compiler ...so this is not allowed
b) since static varibale are class variable and the original variable itself is shared among all the instances(not copy), so for generics it will be difficult for the compiler to predict the type of variable if there are multiple instance ie. Gen<Integer> g=new Gen<>(12); Gen<Stringr> g=new Gen<>("hello");
so now it is impossible for compiler to know about the type of the variable obj .

5. it is valid but,
is not a valid class..why super is not allowed here ??
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
one more....
how can i see the type erased code by the compler??
i tried
javac Gen.java
javap Gen

but is not working. It simply showing T type for the unbounded type, should show Object
 
author & internet detective
Posts: 41403
853
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this post didn't get a reply because it is a bit overwhelming. It is five questions in one post. In the future, please create five threads - one per question. Anyway:

1) I don't know.
2) Just that one is static and one is an instance method. It is rare to have types declared on methods on an instance method. Usually they would be declared on the class itself.
3) Your reason is correct.
4) Because generics imply the type can vary. A static variable is shared by all instances and therefore needs to have one known type.
5) What use would you have to use super there?
 
Sheriff
Posts: 9697
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For 5, to add to what Jeanne said, there is no value addition if you are using <T super Number>. Let's first see what is the advantage of having <T extends Number>

Here since you declared that <T extends Number>, in the method incrementAndPrint you'll know you are getting an object which IS-A Number. So you can call the intValue method on it which is part of Number class. But if you are allowed to use <T super Number>, then you'll not get any value addition:

In this case where <T super Number>, in the method incrementAndPrint you don't know if num is of type Number or Object. So you can only call methods of Object class on it. Thus declaring <T super Number> is the same as <? super Number>, because the only guarantee you'll have is that T IS-A java.lang.Object which is the case for ? as well.

As for checking the compiled code, you might try some decompiler but I'm not sure what exactly you want to see in the compiled code...
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you Jeanne and Ankit

Jeanne Boyarsky wrote:I think this post didn't get a reply because it is a bit overwhelming. It is five questions in one post. In the future, please create five threads - one per question. Anyway:

i will keep in my mind next time

Ankit Garg wrote:As for checking the compiled code, you might try some decompiler but I'm not sure what exactly you want to see in the compiled code...



i want to see the type erased code of this which should be
 
Sunglasses. AKA Coolness prosthetic. This tiny ad doesn't need shades:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic