Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assignment doubt

 
sridhar row
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
source : self


Can someone explain why we can't assign 128 to Long reference variable l1 and we can assign 500 to Short reference variable s1. Also, can someone explain why on line1 its expecting short and in line 3 its not expecting a long.
[ May 26, 2008: Message edited by: sridhar row ]
 
Noam Wolf
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Short constructor takes a String or a short value, since 500 is neither the compiler is giving you the error.

Long l1 = 128; can be read as:

assign the value of 128 (int) to Long l1. Since you aren't casting any number will automatically be an integer. So if you want line 4 to work



you need to modify it in one of the following ways (without using the new Long() constructor):


[ May 26, 2008: Message edited by: Noam Wolf ]
 
sridhar row
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Long l1 = 128; can be read as:

assign the value of 128 (int) to Long l1. Since you aren't casting any number will automatically be an integer. So if you want line 4 to work


Thanks Noam for the reply. But how come we are able to assign long l1 = 128 without doing any of the below.

Long l1 = 128L;
Long l1 = 128l;
Long l1 = (long)128;

Why this difference when assigning to a primitive type and Wrapper type?

and why it is ok to do this:
The constructor takes long and String but the value 128 is an int.
[ May 26, 2008: Message edited by: sridhar row ]
 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sridhar,

The reason you can do


is because of a special exception for Assignment Conversions only, stated in the Java Language Specification:

In addition, if the expression is a constant expression (�15.28) of type byte, short, char or int:
  • A narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable.
  • 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.

    The reason you can do

    is that the int can be widened to a long, and thus is acceptable for the constructor signature Long(long value). You can't do


    for the opposite reason; you can't automatically narrow the integer. The automatic conversion listed above is only for assignment (Short s1 = 500 ;) .

    As an aside, when using the primitive wrapper classes, it's generally preferable to use (for example) Long.valueOf(long); this will let the compiler take advantage of cached values when possible, thus minimizing the creation of new objects.
    [ May 27, 2008: Message edited by: Stevi Deter ]
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 49411
    62
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Noam Wolf:

    Long l1 = 128l;
    Always use upper-case L for a long literal, never lower-case l because it is easy to confuse with 1. With Dd for doubles and Ff for floats there is no risk of such confusion.
     
    Kshitija Mitter
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi,

    Is there any decent knack to remember or soak in the literals assignemnts?
     
    sridhar row
    Ranch Hand
    Posts: 162
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks all.
     
    Deepak Chopra
    Ranch Hand
    Posts: 433
    Eclipse IDE Firefox Browser Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Let me try to put it into more simple words,

    Here comes the concept of compile time creation of Object and run time creation of Object.
    when we write this:

    Short s = 500;

    The Object s is created at compile time, Being a friend compiler try to prevent run time error. so at the compile time, it make a check if the right hand side value can be narrowed to Short, it will allow. Same is this case.

    When we write following :

    Short s = new Short(500);

    In this case, the object will be created at run time, so Compiler thinks that the int value(500) we are using to create Object may or may not be sufficient to fit into short. So there is probability of getting run time error.
    Thats why it doesn't allow to do that.

    When we write following :

    Long l = 500;

    this come out to be case of AutoBoxing. Since in auto boxing,
    Broadening --> Casting
    is not allowed, so compiler gives you an error.

    When we write following :

    Long l = new Long(500);

    this is run time creation of Object, Compiler knows that whatever be the value of int, it will ALWAYS fit into long. so it allows.
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 49411
    62
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Kshitija Mitter:
    Hi,

    Is there any decent knack to remember or soak in the literals assignemnts?
    Same as for remembering anything else: write lots of code and see how it works.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic