Home arrow static arrow Java Programming [Archive] - Java 1.5 autoboxing and conversion
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Java 1.5 autoboxing and conversion
This topic has 7 replies on 1 page.

Posts:7
Registered: 4/25/04
Java 1.5 autoboxing and conversion  
Aug 2, 2004 2:29 AM



 
Hi,

What is the correct cast sequence for this.
Short s;
Integer i = 8;

s = (short)(int) i;

This works, but seems somewhat ackward. Any better?

-- Kasper
 

Posts:319
Registered: 11/8/00
Re: Java 1.5 autoboxing and conversion  
Aug 2, 2004 2:54 AM (reply 1 of 7)



 
with 'Tiger' the autoboxing will allow you to go stright from an Integer to a short.

but in 1.4 this might be better, if you are talking about the primative type short

short s = (short)i.intValue();


If your talking about the Short object you want
Short s = i.shortValue();
 

Posts:319
Registered: 11/8/00
Re: Java 1.5 autoboxing and conversion  
Aug 2, 2004 2:59 AM (reply 2 of 7)



 
Soz that should have been

Short s = new Short(i.shortValue());
 

Posts:319
Registered: 11/8/00
Re: Java 1.5 autoboxing and conversion  
Aug 2, 2004 3:00 AM (reply 3 of 7)



 
and again, i wish you could edit there posts

short s = i.shortValue();
 

Posts:4,496
Registered: 19/06/02
Re: Java 1.5 autoboxing and conversion  
Aug 2, 2004 3:10 AM (reply 4 of 7)



 
What is the correct cast sequence for this.
Short s;
Integer i = 8;

s = (short)(int) i;

This works ...

That code will not compile.

with 'Tiger' the autoboxing will allow you to
go stright from an Integer to a short.

I don't think so.
short s;Integer i = new Integer(8);s = i;

This code doesn't compile with 1.5.
 

Posts:319
Registered: 11/8/00
Re: Java 1.5 autoboxing and conversion  
Aug 2, 2004 3:24 AM (reply 5 of 7)



 
with 'Tiger' the autoboxing will allow you to
go stright from an Integer to a short.

I don't think so.

Just what i read in a book, i have not actually tried it yet.

 

Posts:4,496
Registered: 19/06/02
Re: Java 1.5 autoboxing and conversion  
Aug 2, 2004 3:50 AM (reply 6 of 7)



 
Just what i read in a book, i have not actually tried
it yet.

Actually looking again, I think you're right. It should work. This looks like a compiler bug ...
 

Posts:4,496
Registered: 19/06/02
Re: Java 1.5 autoboxing and conversion  
Aug 2, 2004 3:58 AM (reply 7 of 7)



 
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4974939

Fixed in tiger-beta2 apparently.
 
This topic has 7 replies on 1 page.