In some code I see this:
private void compute(Long a, Long b, Long c) {
long result = a-(b+c);
...
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
The following line:
long result = a-(b+c);
...asks Java to take the result of the expression using 3 Long
s, 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.
Based on your needs.I mean the decelaration.
Autoboxing
and unboxing
can happen anywhere where an object is expected and primitive type is available
From Java 1.5 onwards, autoboxing and unboxing occurs implicitly whenever needed.
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:
Long
values.List
, Map
), which cannot hold primitives.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.
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