• 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
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

Simple autoboxing question

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Why does the first line not compile while the second does?

Long L1 = 23;
Short S1 = 23;

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dude you are not correctly Auto Boxing.. Long L1 =23; is wrong method...

it should be like this.. Long L1 = new Long(23);...

or you can use long L1 = 23; .... its simple long variable...
 
Erez Pitchon
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, but the response didn't answer my question. I could also re-write the code to compile,but that doesn't give me any insight as to why one line compiles while the other does not.
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Erez Pitchon wrote: Long L1 = 23; //Line 1
Short S1 = 23;



The 23 in Line 1 is integer literal. For Long, you need to write 23L for the compiler to understand it is a Long value. Short s1 = 23 is acceptable since it occupies less memory than an integer.

Don't know should i say it is naming convention or what. That's the only reason i can see why it is not compiling.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Erez Pitchon wrote:Hi

Why does the first line not compile while the second does?

Long L1 = 23;
Short S1 = 23;



Hi,
I think that the second line compiles because the compiler implicitely reconizes that 23 fits into short and then boxes it to Short. In case of the first line things get a bit tricky nonetheless one should remember that you can only box and then widen not the other way around so this time the compiler can only try to box the int type to Integer and then try to widen to Long. The last operation failes since Long and Integer hold the same level in the type hierarchy, I mean they are not in the same inheritance tree path (neither of them extends the other).

Regards,
skyeweaver
 
Erez Pitchon
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The 23 in Line 1 is integer literal. For Long, you need to write 23L for the compiler to understand it is a Long value. Short s1 = 23 is acceptable since it occupies less memory than an integer.



Since you say that 23 in line 1 is an integer (that I know), this should be true in both lines initially. Hold that thought. Now regarding line 2, you ment to say 23 is acceptable sice it occupies les memeory than a short? If that is true, than 23 should be acceptable in both lines because in both cases it occupies less memory than the variable type it is being assigned to.

Now just like you say that you need "L" for the compiler to understand the value as long for the second line, so you should need an indication for the compiler to understand it as short for the first line if we say that both lines start out as integer values.

So I'm still not happy with the response. :-(
 
author
Posts: 23931
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, it does seem silly that the compile time constant assigment checks doesn't applies to longs. I guess it wasn't necessary as implicit casting was available.... but of course, doesn't work when you throw autoboxing into the mix.

Oh well, maybe it will be fixed in a future release.

Henry
 
Erez Pitchon
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understood what skyeweaver said with regard to the Long and that makes perfect sense to me with box-then-widen, widen-then-box rules. I'm not sure I understand why the same does not apply to the Short using the same logic. Can someone name the steps the compiler would take on any such assignment that could account for these two cases acting as they do?

WrapperClassName varName = int_value; /* for example: Long L1 = 23; Short S1 = 23; */

compiler logic:
1.
2.
3.
...
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Erez,

Instead of following the compiler logic, I'm going to explain what's going on just by following the Java Language Specification regarding assignment conversion (that is, conversions allowable in the context of an assignment statement.) You can read the details in the JLS, section 5.2.


In this case, you have a literal which is of type int. The only way that you can assign that to Long would be to do primitive widening from int to long, then box the long to Long. But you can't do widening plus boxing. It's not allowed. And I believe there are good reasons for that (possibly overloading resolution considerations, but let's stick to the point.)


In this case, 23 is a literal of type int. It's also a compile time constant which is in the range of short, so first a primitive narrowing conversion from int to short will take place, and then a boxing conversion from short to Short will take place. This is explicitly stated in the JLS:


• A narrowing primitive conversion followed by a boxing conversion may be
used if the type of the variable is :
- Byte and the value of the constant expression is representable in the type
byte.
- Short and the value of the constant expression is representable in the type
short.
- Character and the value of the constant expression is representable in the
type char.


Edit: I forgot to add that this latest case won't work in a method invocation conversion (In other words, if you have a method that expects a Short, you can't call it passing the literal 23.) That's similar to what happens with implicit narrowing conversion of primitives. You can say:
short s = 23;
But you can't call this method:
void someMethod(short s){}
this way:
someMethod(23);
 
Erez Pitchon
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yey Ruben Soto. That is solid. Thanks muchly.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure thing Erez!
Remember: When in case of doubt, consult the JLS. There is no point in trying to guess how things work, because there is a high degree of arbitrariness in the definition of any programming language.
 
reply
    Bookmark Topic Watch Topic
  • New Topic