當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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

參考: https://docs.oracle.com/javase/7/docs/api/java/lang/Long.html#longValue()



相關用法


注:本文由純淨天空篩選整理自ankita_chowrasia大神的英文原創作品 Long longValue() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。