| Author |
Long s = 1 vs Short s = 1 - compiler error
|
Larry Olson
Ranch Hand
Joined: Feb 03, 2009
Posts: 142
|
|
Why does
gives a compiler error saying:
found : int
required: java.lang.Long
Long s = 1;
^
whereas
compiles fine? Shouldn't it complain something like:
found : int
required: java.lang.Short
Long s = 1;
^
One of those confusing things I could never get into my brain.
Thanks.
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
This is because, you can't do widening and Boxing.
If the int value in the range of short, there will be a narrowing. So it is OK.
Hope you understood~!
|
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
|
 |
Larry Olson
Ranch Hand
Joined: Feb 03, 2009
Posts: 142
|
|
Thanks. It is confusing, but I think I understand. So I guess the principle is:
Widening and Boxing => NOT ALLOWED
Boxing and widening => ALLOWED
Somehow I have to burn it into my brain.
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
Larry Olson wrote:Thanks. It is confusing, but I think I understand. So I guess the principle is:
Widening and Boxing => NOT ALLOWED
Boxing and widening => ALLOWED
Somehow I have to burn it into my brain. 
Yea...
For your convenient...
Widening, Boxing, Var-arg
1) Primitive Widening > Boxing > Var-args
2) Widening and Boxing (WB) not allowed.
3) Boxing and Widening (BW) allowed.
4) While overloading, Widening + Var-arg AND Boxing + Var-arg can only be used in a mutually exclusive manner(NOT together)
5) Widening between Wrapper class are not allowed...
Hope this will help......
|
 |
Emmanuel Aron
Greenhorn
Joined: Apr 28, 2010
Posts: 16
|
|
and if the "machine" tries to box and wide (ie: Integer k = 1; and Long l = k;),there will be an error when trying to put Integer into Long.
Am i right?
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
While this topic is interesting, I did notice that none of the answers addressed the original question... This..
is narrowing (int to short), followed by boxing (short to Short). And the reason that it is allowed is due to a part of the JLS related to compile time constants.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Larry Olson
Ranch Hand
Joined: Feb 03, 2009
Posts: 142
|
|
Hi Henry,
Thanks as always for sharing your expertise. In a nutshell, are we saying:
Widening and Boxing => Not allowed
where as
Narrowing and Boxing => Allowed
???
Thanks.
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
Larry Olson wrote:
Thanks as always for sharing your expertise. In a nutshell, are we saying:
Widening and Boxing => Not allowed
where as
Narrowing and Boxing => Allowed
???
Yes, it is a bit weird that, in this example, implicit widening following by boxing is not allowed, while implicit narrowing following by boxing is allowed. You just have to remember that the JLS has quirks and this is one of them. Also, it may be best to not remember it as "narrowing and boxing is allowed", but as the rules for compile time constants... as not all cases of "narrowing and boxing" are allowed.
Specifically, section 5.2 of the JLS, about a few paragraphs down...
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.
Henry
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
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.
Henry, could you please explain what is mean by constant expression? with related to what? Thanks in Advanced!
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
Abimaran Kugathasan wrote:
Henry, could you please explain what is mean by constant expression? with related to what? Thanks in Advanced!
For that, you can start with section 15.28 of the JLS. Or you can start with a small writeup of compile time constants that I did a while back...
http://www.coderanch.com/t/454384/java/java/compile-time-constant
Henry
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
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.
Sorry Henry, I don't mean that, I do mean in the above para, what is mean by constant expression, is it related to switch expression?
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
Abimaran Kugathasan wrote:
Sorry Henry, I don't mean that, I do mean in the above para, what is mean by constant expression, is it related to switch expression?
The topic that I pointed to didn't answer your question? Can you elaborate why? Saying "don't mean that" doesn't do anything since they look like the same thing to me.
Henry
|
 |
 |
|
|
subject: Long s = 1 vs Short s = 1 - compiler error
|
|
|