Java中的 Long longValue() 方法
Java.lang.Long.longValue() 是Java Long 类的一个内置方法,它在转换后将这个Long 对象的值作为long 返回。
句法:
public long longValue()
参数:此方法不带任何参数。
返回值:该方法将返回该对象所代表的数值,转换为long类型。
例子:
Input: 5366623
Output: (Long) 5366623
Input: -6723887
Output: (Long) -6723887
Explanation:
When the number is passed in this object it will convert that
to long and gives the value like,
Long lobject = new Long(5366623)
It will return 5366623 as long.
下面的程序说明了Java.lang.Long.longValue() 方法的工作原理。
程序 1:对于正数。
java
// Java program to illustrate the
// java.lang.Long.longValue() method
import java.lang.*;
public class Geek {
public static void main(String[] args)
{
Long lobject = new Long(77387187);
// It will return the value of this Long as a long
long nl = lobject.longValue();
System.out.println("The Value of nl as long is = " + nl);
Long lobject2 = new Long(-6723887);
// It will return the value of this Long as a long
long nl2 = lobject2.longValue();
System.out.println("The Value of nl2 as long is = " + nl2);
}
}
java
// Java program to illustrate the
// java.lang.Long.longValue() method
import java.lang.*;
public class Geek {
public static void main(String[] args)
{
Long lobject = new Long(97387717187);
// Very large number will produce compile errors
long nl = lobject.longValue();
System.out.println("The Value of nl as long is = " + nl);
}
}
输出:
The Value of nl as long is = 77387187
The Value of nl2 as long is = -6723887
方案 2:对于一个非常大的号码。
// 会产生编译时错误。
Java
// Java program to illustrate the
// java.lang.Long.longValue() method
import java.lang.*;
public class Geek {
public static void main(String[] args)
{
Long lobject = new Long(97387717187);
// Very large number will produce compile errors
long nl = lobject.longValue();
System.out.println("The Value of nl as long is = " + nl);
}
}
输出:
prog.java:9: error: integer number too large: 97387717187
Long lobject = new Long(97387717187);
^
1 error
参考: https: Java/lang/Long.html#longValue()