aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Assignment doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Assignment doubt" Watch "Assignment doubt" New topic
Author

Assignment doubt

sridhar row
Ranch Hand

Joined: Jan 16, 2008
Posts: 162
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

Joined: Jan 12, 2008
Posts: 35
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 ]

because .net guys can also write in java
sridhar row
Ranch Hand

Joined: Jan 16, 2008
Posts: 162
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

Joined: Mar 22, 2008
Posts: 265

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 ]

    There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39828
        
      28
    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

    Joined: Mar 03, 2008
    Posts: 11
    Hi,

    Is there any decent knack to remember or soak in the literals assignemnts?
    sridhar row
    Ranch Hand

    Joined: Jan 16, 2008
    Posts: 162
    Thanks all.
    Sunny Jain
    Ranch Hand

    Joined: Jul 23, 2007
    Posts: 433

    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.


    Thanks and Regards,
    SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39828
        
      28
    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.
     
    wood burning stoves
     
    subject: Assignment doubt