• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Assignment doubt

 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 265
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
     
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks all.
     
    Ranch Hand
    Posts: 433
    Eclipse IDE Firefox Browser Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic