This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Long s = 1 vs Short s = 1 - compiler error

 
Larry Olson
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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~!
 
Larry Olson
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20894
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Larry Olson
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20894
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 2066
    Clojure IntelliJ IDE Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Marshal
    Pie
    Posts: 20894
    76
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 2066
    Clojure IntelliJ IDE Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    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
    Marshal
    Pie
    Posts: 20894
    76
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic