问题
Why has the java class Long a method longValue()? And what is the use of that? The java doc says
the numeric value represented by this object after conversion to type long.
Is there a case where I need this? Could the following first code code differ from the second for any long value?
//Is there a value where this myLong value contains other...
Long l = new Long("...");
long myLong = l;
//...than this one?
Long l = new Long("...");
long myLong = l.longValue();
回答1:
Before Java5 there is No Autoboxing and Unboxing so that longValue() method casts to primitive long and returns.
public long More ...longValue() {
722 return (long)value;
723 }
So there is no specific difference. Any way after java5 the below code is enough
Long l = new Long("...");
long myLong = l;
回答2:
Long extends Number which has an abstract long longValue() method. So Long must implement it, the same way it implements doubleValue for example.
When you have a Long aLong you can always write long primitive = aLong; and use auto-boxing, so the method is not very useful in the Long class.
回答3:
It's a old method, introduced in the first version of Java.
Back in the day, there were no outbox/autoboxing features in the language (they were only introduced in Java 5.0), and this was the only way to get a long from a Long.
来源:https://stackoverflow.com/questions/20922867/java-class-long-method-longvalue