java long object_Java Long longValue()用法及代码示例

java.lang.Long.longValue()是Java中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 praogram 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);

}

}

输出:

The Value of nl as long is = 77387187

The Value of nl2 as long is = -6723887

示例2:对于非常大的数字。

//将产生编译时错误。

// Java praogram 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值