• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Question about Autoboxing/Unboxing.

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I'm about to take the SCJP in the next few months and is reading a chapter in the book about autoboxing/unboxing. Having read the chapter, I decided to try it out in code and found something that I do not quite understand...

These lines apparently fails to compile:
short s = 2;
Integer x = (short)s; //cannot convert from short to Integer

byte b = 2;
Integer y = (byte)b; //cannot convert from byte to Integer

Long lx = (int)32; //cannot int to Long. Long lx = 32 doesn't work either.

However, these actually worked:

Integer x = (short)2;
Integer y = (byte)2;

Why is this? Why does "casting" an int with short or byte allowed it to be converted? Then why doesn't Long lx = (int)32; work?

Very confused.

[ June 30, 2007: Message edited by: Hugh Kwong ]
[ June 30, 2007: Message edited by: Hugh K ]
 
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 Hugh Kwong:
Integer x = (short)2;
Integer y = (byte)2;

These two are invalid too.
 
Hugh Kwong
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Manfred Klug:
These two are invalid too.



But it compiles and runs fine with these two.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Hugg,

The Code you have given above nither works;

Because while doing Boxing,or unboxing you have to remember that "YOU CAN BOX AND THEN WIDEN" but " YOU CANNOT DO WIDENNING AND THEN BOX"

short s=2;
Integer i=(short)s;

Here first ,the compiler tries to box short to Short and then widen to Integer ,But Integer is not the Super of Short ,so it is happy to give an ERROR!!

If you try with Number in place of Integer it works!! Try it . As Number is Super type of Short...
 
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 Hugh K:
But it compiles and runs fine with these two.

Seems that one of our compilers has a bug. I have retested it with three different compilers.

Sun's 1.5.0_11 version reports an error.
Eclipse 3.2.2 and 3.3.0 works without error.

According to the JLS, I would say that the error is correct, since boxing from short or byte to Integer is not possible.

Interestingly, 'Long lx = (int)32;' fails with all three.
[ July 01, 2007: Message edited by: Manfred Klug ]
 
Hugh Kwong
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys! That answered my question.

The book wasn't so clear about the behavior of boxing/unboxing at assignments (it only covered the box-->widening on overloading..), so I thought it might have worked. And to add to the confusion, I was using Eclipse 3.2.x to compile...
[ July 01, 2007: Message edited by: Hugh K ]
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers,


none of these lines will compile with the original java compiler from Sun. Because, as Arun already said: "YOU CANNOT DO WIDENNING AND THEN BOX".toUpperCase();

Due to a bug, the short and byte version do compile in Eclipse.
Manfred tested Eclipse 3.2.2 and 3.3.0, and I just tested 3.1.0, same results.

I didn't know this before.
What I knew before:
Perhaps, when training for SCJP, it may be better to use the original compiler...


To Hugh:

Welcome to the Ranch!!!



And go on with your experiments, you'll learn a lot.



Only one small issue: the Javaranch has a policy regarding user names. Please check this:
http://www.javaranch.com/name.jsp

and could you please change your name?


Yours,
Bu.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hugh,

that was really fast, you changed your name already.


Yours,
Bu.
 
She still doesn't approve of my superhero lifestyle. Or this shameless plug:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic