Can someone help me understand this? (java generics wildcards)

Hi all.
First of all this is my first post on sun forums. Besides that i only used java for few weeks now and i think java is quit a nice language.
I do have a question regarding java generics and wildcard casting (if you can call it that).
Assume you have following code.
public class Test <T extends Number> {
     private T x;
     Test(T x)
          this.x = x;
     T getX()
          return x;
     <U extends Number> void setX(U x)
          this.x = (T)x;
public class Main {
     public static void main(String[] args) {
          Test<Integer> p = new Test <Integer>(5);
          System.out.println(p.getX());
          p.setX(53.32);
          System.out.println(p.getX());
}This compiles with following results:
5
53.32
Question:
First: If I declared p to be of type Test<Integer> then why did x in Test class changed its type from Integer to Double?
Second: Is this a possible unchecked casting behavior? and if so why does this work but doing something like this :
Integer x = 5 ;
Double y = 6.32;
x=(Integer)y;fails with compile time error?

Hello and welcome to the Sun Java forums.
The behavior you describe would not occur if your setX(?) method used T instead of another type constraint.
However, let's look at why it works!
At line 1 in main, you create your Test with a type parameter of Integer; this becomes the type variable T for that particular instance of Test. The only constraint is that the type extends/implements Number, which Integer does.
At line 2 you obtain a reference to a Number, which is what getX() is guaranteed to return. toString() is called on that object, etc.
At line 3 you set the Number in the Test to a Double, which fits the type constraint U extends Number, as well as the type constraint T extends Number which is unused (!). Note that using a different type constraint for U, such as U extends Object or U super Number, could cause a compile-time error.
At line 4 you obtain a reference to a Number, which is what getX() is guaranteed to return.
The quirky behavior of setX is due to the second type constraint, which is obeyed separately; in this case, you probably want to use void setX(T x) {
this.x = x;
}edit: For the second part of your question, x = (Integer)y; fails because Integer and Double are distinct types; that is, Integer is not a subclass of Double and Double is not a subclass of Integer. This code would compile and work correctly: Number x = 5 ;
Double y = 6.32;
x=y;s
Edited by: Looce on Nov 15, 2008 7:15 PM

Similar Messages

Maybe you are looking for

  • JNI Hotspot Error: Access Violation, need help interpreting the log.

    Hello. I'm a relatively new developer (previously an engineer), and so to start me off I was given the task of providing a JNI-enabled interface to some legacy licensing software (it's C++, we want Java). Now the functionality of JNI is not the probl

  • Email password( AOL)

    To the Apple tech specialist,or any Mac specialist.... Is there a way you can color code the little blue icon that indicates a New mail in the folder to indicate read already by someone else so that if the person hits the save email as unread it does

  • ADF11g: business logic in ADF BC or PLSQL?

    Hi All, For a new development in ADF 11g , where we should put our business logic: In ADF BC or in PLSQL? What if we write all the bussiness logic in plsql? Because if tomorrow ADF goes(which I know is not ging to happen soon), we can expose pl/sql a

  • Attachment download link

    What is the best way to have a download link on a page? I searched for this topic and found a few posts that used h:commandLink. A backing-bean method gets invoked with code something like as follows.       FacesContext facesContext =  FacesContext.g

  • Import avi video with uncompressed PCM audio

    I am unable to import into iMovie 10 or FCP X the audio portion of avi videos with uncompressed PCM audio. I just get the video and silence. They imported fine into previous versions of iMovie. Of course, I can use conversion programs to produce vers