Unboxing Long in java

前端 未结 9 1318
别跟我提以往
别跟我提以往 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: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

提交回复
热议问题