Java8 基礎數據類型包裝類-Long


 

基礎

//final修飾不可更改,每次賦值都是新建類(其中-128~127是通過LongCache數組獲取的不是新建的,所以可以使用==比較,但其他數據是通過new創建的,不能使用==直接比較大小,因為是不同的類,地址不同,需用equals) public final class Long extends Number implements Comparable<Long> {}
  • 1
  • 2

常量

//-2^63 @Native public static final long MIN_VALUE = 0x8000000000000000L; //2^63-1 @Native public static final long MAX_VALUE = 0x7fffffffffffffffL; //位數 @Native public static final int SIZE = 64; //字節數 public static final int BYTES = SIZE / Byte.SIZE;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

繼承 
抽象類Number 
獲取包裝類與基本類型之間的轉換值,short、int、long、byte、float、double。 
實現 
Comparable 接口 
實現compareTo(T o)方法 
私有靜態內部類

//初始化是-128~127的Long對象 private static class LongCache { private LongCache(){} static final Long cache[] = new Long[-(-128) + 127 + 1]; static { for(int i = 0; i < cache.length; i++) cache[i] = new Long(i - 128); } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

方法

轉成特定進制的字符串

//i:數值 //radix:需要轉換的進制(2~36 不在此范圍的按10進制處理) public static String toString(long i, int radix) {} //默認轉成10進制,當i==MIN_VALUE 直接返回數值"-9223372036854775808" public static String toString(long i) {} //調用toString(long i) public String toString() {}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

按無符號數值轉特定進制的字符串,BigInteger

//無符號數轉字符串 //i:數值 //radix:需要轉換的進制(2~36 不在此范圍的按10進制處理) public static String toUnsignedString(long i, int radix) {} private static BigInteger toUnsignedBigInteger(long i) {} //轉化成16進制無符號字符串 public static String toHexString(long i) {} //轉化成10進制無符號字符串 public static String toUnsignedString(long i) {} //轉化成8進制無符號字符串 public static String toOctalString(long i) {} //轉化成2進制無符號字符串 public static String toBinaryString(long i) {}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

字符串轉long

//s:數值字符串 //radix:s字符串是幾進制(radix不在2~36則拋異常) public static long parseLong(String s, int radix) throws NumberFormatException{} //字符串為10進制 public static long parseLong(String s) throws NumberFormatException {} //無符號字符串轉long(字符串第一位為“-”拋異常) //radix:表示s字符串是幾進制(radix不在2~36則拋異常) public static long parseUnsignedLong(String s, int radix) throws NumberFormatException {} //字符串為10進制 public static long parseUnsignedLong(String s) throws NumberFormatException {}

字符串,long轉Long

//s:數字型字符串 //radix:表示s字符串是幾進制(radix不在2~36則拋異常) public static Long valueOf(String s, int radix) throws NumberFormatException {} //默認為10進制 public static Long valueOf(String s) throws NumberFormatException{} //其中-128~127的Long是程序啟動時就已經保存到LongCache中,所以在這個范圍的數據是直接取的LongCache中的值 public static Long valueOf(long l) {} //nm:可以是010(8進制)、0x10(16進制)(#開頭的在這個方法中也表示16進制) public static Long decode(String nm) throws NumberFormatException {}

   
   
   
           

Long轉基本數據類型

public byte byteValue() {} public short shortValue() {} public int intValue() {} public long longValue() {} public float floatValue() {} public double doubleValue() {}

   
   
   
           

獲取系統屬性的值

//nm:系統屬性的名稱 //val:如果為空時的默認值 public static Long getLong(String nm, long val) {} public static Long getLong(String nm, Long val) {} //返回默認值為null public static Long getLong(String nm) {}

   
   
   
           

比較及求和

//返回-1,1,0(-1表示y大,1表示x大) public int compareTo(Long anotherLong) {} //返回-1,1,0(-1表示y大,1表示x大) public static int compare(long x, long y) {} //比較xy無符號數的大小,返回-1,1,0(-1表示y大,1表示x大) public static int compareUnsigned(long x, long y) {} public static long sum(long a, long b) {} public static long max(long a, long b) {} public static long min(long a, long b) {}

   
   
   
           

無符號數運算

//將dividend和divisor轉成無符號整數相除取整 public static long divideUnsigned(long dividend, long divisor) {} //將dividend和divisor轉成無符號整數相除取余 public static long remainderUnsigned(long dividend, long divisor) {}

    
    
    
            

位運算 
同Integer


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM