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
-
I have turned off the Data use on the I-phone 4s but am told by AT&T that data is still being used for applications. How is this possible. Can someone help me understand this.
I assume you are including the STIX font as part of your epub files?
Perhaps the folks who do this blog might be able to help -- they have done some work with font embedding:
http://www.pigsgourdsandwikis.com/2011/04/embedding-fonts-in-epub-ipad-iphone-an d.html -
my ipod touch 1st gen has gone into recovery and wont restore because of a error 1 or something like that can someone help me quick this ipod is my little cousins and i dont want her mum to find out what happend
I have not seen a solution for error (1)
make an appointment at the Genius Bar of an Apple store since it appears you have a hardware problem.
Apple Retail Store - Genius Bar -
Some of my photos in Iphoto turn blank (white) when I tried to edit the size like zoon in and out.Can someone help me on this?
As a Test:
Hold down the option (or alt) key and launch iPhoto. From the resulting menu select 'Create Library'
Import a few pics into this new, blank library. Is the Problem repeated there? -
So i have an apple ipod touch 1st generation and it is not charging! It shows the charging sign but its taking a really long time to charge. Ive had it charging for at least 3 hours and it still says under 20% so can someone help me figure this out??
Not Charge
- See:
iPod touch: Hardware troubleshooting
iPhone and iPod touch: Charging the battery
- Try another cable.
- Try another charging source
- Inspect the dock connector on the iPod for bent or missing contacts, foreign material, corroded contacts, broken, missing or cracked plastic.
- Make an appointment at the Genius Bar of an Apple store.
Apple Retail Store - Genius Bar -
I have iOS 7. When I go to the itunes store on my ipod touch it gives me a blank screen with nothing on it. I restarted my ipod touch and it didn't work. Can someone help me with this?
- Reset the iOS device. Nothing will be lost
Reset iOS device: Hold down the On/Off button and the Home button at the same time for at
least ten seconds, until the Apple logo appears.
- Go to Settings>iTunes and App stores and sign out and sign back in
- Reset all settings
Go to Settings > General > Reset and tap Reset All Settings.
All your preferences and settings are reset. Information (such as contacts and calendars) and media (such as songs and videos) aren’t affected.
- Restore from backup. See:
iOS: How to back up
- Restore to factory settings/new iOS device. -
HT3529 Can someone help me understand why my iMessage no longer works on my iPad
Can someone help me understand why my iMessage no longer works on my iPad
iOS: Troubleshooting Messages
iOS: Troubleshooting FaceTime and iMessage activation -
The calender on my iPhone 4 with iOS 6 no longer syncs with iCal 4.0.4 on my mac OS 10.6.8. However it is syncing with my iCloud account. Can someone help me with this?
FYI: It seems it was a network problem with my WinXP PC. After complete deletion and new installation of XP and iTunes my iPhone will be recognized correctly, Wifi synchronization works (mostly) fine.
-
I try to sync my nano and get: the ipod cant be synced because there is not enough free space to hold all of the items in the items library (need 100MB) - I have a new computer?? can you help me understand this message: what to do?
Hello pryan1012,
What this message means is that you have more music in your itunes library than there is free space in your ipod.
I had this same issue at one time. This is what helped me put my music on the ipod. I used manually manage.
Learn how to sync muisc here.
Hope this helps.
~Julian -
I'm trying to clean my mailbox and when I click on delete or highlight and move to trash I get a message that says "operation could not be completed - file exists. Further, when I select a message or group of messages, one of them is copied (not deleted) to the trash folder. Can someone help me through this?
Thanks.It's a POP account. I decided to clean up my mailbox today after noticing the computer was real slow after USA Today started sending their daily top stories with new graphics. Also before I noticed the problem with mail deletion - two things happened. 1) I updated security sofware as prompted by Apple and 2) downloaded Mackeeper and went throught some preliminary scans but did not buy their package. Thanks forn nstaying with me.
-
I'm having issues loading Youtube on my MacBook Pro for about a week now. I'm not sure if this is because of my computer or internet but my sister's PC loads Youtube completely fine. Can someone help me figure this out?
Hi, I had been having the same problem for over a week, but both my window's HP and my 2011 macbook pro barely could load youtube. What kind of router you have? I was using a belkin play router that was only 8 months old, switched to a new AirPort Extreme Base Station and now everything works perfect.
-
i found an old gift card that i got with my ipod from spain
i have a canadian account. when i tried using the gift card it said i couldn't use the spanish gift card with my canadian account. can someone help me solve this problem
the gift card is 15 eurosSorry, but you cannot use that card with a Canadian iTunes Store account. A gift card is valid only in the country of purchase. You'll need to give or sell that card to someone in Spain.
Regards. -
I have a problem with my blackberry 9360, I bought it last 2months, and now when I use video camera or camera I saw a red and blue line. Can someone help me with this issue?
Hello zukafu,
Welcome to the forums.
In this case we can try to reload the software on the BlackBerry® smartphone to make sure it is running the latest version and there are no issues with the OS.
Here is how to backup your BlackBerry smartphone http://bbry.lv/oPVWXc
Once you have backed up your BlackBerry smartphone please follow the link below to complete a clean reload of the BlackBerry smartphone software.
Link: http://www.blackberry.com/btsc/KB03621
Once completed test it and proceed with a selective restore, here is how to restore http://bbry.lv/qgQxLo
Thank you
-SR
Come follow your BlackBerry Technical Team on twitter! @BlackBerryHelp
Be sure to click Kudos! for those who have helped you.Click Solution? for posts that have solved your issue(s)! -
I have followed the instructions to update Flash Player, the Installation Fails at about 90%, it says that there is an Application running (Safari) and it says to close all the apps. and start again ... but I already close all the Applications ... none is running ... can someone help me with this issue ??? Thanks ...
Dear Dominic
Brilliant reply. Simple English. Simple to follow instructions. And it worked immediately, first time.
Why couldn't the Apple and Adobe corporations get their programming right first time? We spend billions of UK pounds and US dollars with them. They reply with incompetent programming such as this, and arrogance to their customers in issuing faulty systems without doing the most rudimentary checks.
Anyway, I certainly shan't be buying another Apple as this is the most unreliable, most incomprehensible, most illogical and downright thoughtless shoddy piece of computer kit which I have ever owned. And all of it is rubbish ~ emails disappear, photos can't be organised properly, spreadsheets don't work, Pages is laborious… the list goes on and on...
But thanks to you Dominic, I have been able to load Adoble Flashj… maybe eyou should get a job at Apple, and set them all on the right course to how to work simply and correctly with customers.
Thanks again,
David -
After the OS 6 update my speaker does not work. Volume control on the side does not adjust the volume either. My music plays but no sound comes out. Can someone help me with this issue? I've tried rebooting my phone but still no sound comes out. I currently have the 4S.
try to activate and desactivate the airplne switch a couple of time
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
-
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