Unboxing Long in java

前端 未结 9 1316
别跟我提以往
别跟我提以往 2021-01-15 16:44

In some code I see this:

private void compute(Long a, Long b, Long c) {
        long result = a-(b+c);
...


        
相关标签:
9条回答
  • 2021-01-15 17:09

    The arithmetic operators + and - are not defined for boxed types (e.g. Long) but for primitive types (e.g. long).

    The result is also a long. See Autoboxing and Unboxing tutorial

    Autoboxing this into a Long would result in a small performance cost. It is also unnecessary because

    1. We know it will be non-null (if a, b or c were null, a NullPointerException would occur).
    2. It would be autoboxed implicitly if we use it later where a Long is required.
    0 讨论(0)
  • 2021-01-15 17:10

    The following line:

    long result = a-(b+c);
    

    ...asks Java to take the result of the expression using 3 Longs, and then store it in a primitive long. Before Java 5, it would complain about the types not matching - but these days it just assumes you mean what you say and automatically does the conversion from object to primitive type for you.

    In this example however, unless there's some other good reason not presented here, there's absolutely no point having the parameters as the boxed, object type in the first place.

    0 讨论(0)
  • 2021-01-15 17:11

    Based on your needs.I mean the decelaration.

    Autoboxing and unboxing can happen anywhere where an object is expected and primitive type is available

    0 讨论(0)
  • 2021-01-15 17:18

    From Java 1.5 onwards, autoboxing and unboxing occurs implicitly whenever needed.

    0 讨论(0)
  • 2021-01-15 17:19

    It seems a bit strange that the result is stored in a primitive long instead of a Long object corresponding to its operands.

    No, what is "strange" is that you can use the + and - operators on Long objects. Before Java 5, this would have been a syntax error. Then autoboxing/unboxing was introduced. What you're seeing in this code is autounboxing: the operators require primtives, so the compiler automatically inserts a call to longValue() on the objects. The arithmetic is then performed on primitive long values, and the result is also a long that can be stored without further conversion on the variable.

    As for why the code does this, the real question is why someone would use the Long type instead of long. Possible reasons:

    • The values come from some library/API that delivers Long values.
    • The values are stored in collections (List, Map), which cannot hold primitives.
    • Sloppiness or cargo cult programming.
    • The ability to have null values is required, e.g. to signal unavailable or uninitialized data.

    Note that the ability of Long to hold null values means that the calculation (or more specifically, the longValue() calls inserted by the compiler) can fail with a NullPointerException - a possibility the code should deal with somehow.

    0 讨论(0)
  • 2021-01-15 17:20

    As per the javadoc

    Boxing conversion converts expressions of primitive 
    type to corresponding expressions of reference type. 
    Specifically, the following nine conversions are called the boxing conversions:
    
    From type boolean to type Boolean
    
    From type byte to type Byte
    
    From type short to type Short
    
    From type char to type Character
    
    From type int to type Integer
    
    From type long to type Long
    
    From type float to type Float
    
    From type double to type Double
    
    From the null type to the null type
    
    
    Ideally, boxing a given primitive value p, would always yield an identical reference.     
    In practice, this may not be feasible using existing implementation techniques. The  
    rules above are a pragmatic compromise. The final clause above requires that certain 
    common values always be boxed into indistinguishable objects. The implementation may  
    cache these, lazily or eagerly. For other values, this formulation disallows any 
    assumptions about the identity of the boxed values on the programmer's part. This would 
    allow (but not require) sharing of some or all of these references.
    
    This ensures that in most common cases, the behavior will be the desired one, without     
    imposing an undue performance penalty, especially on small devices. Less memory-limited 
    implementations might, for example, cache all char and short values, as well as int and 
    long values in the range of -32K to +32K.`
    

    Here is the Oracle Doc source

    0 讨论(0)
提交回复
热议问题