Java Class Long - method longValue()

独自空忆成欢 提交于 2019-12-12 19:31:43

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!