歸納
- Integer和Long都繼承自Number類,該類的方法均為類型轉換,如intValue()
- 均為有符號整數,大小分別為
-2^31~2^31-1
和-2^63~2^63-1
- Integer和Long都可以轉為多種進制的字符串,靜態方法toString(int,radix)
- 多種進制的字符串轉為Integer和Long,使用valueOf(String,radix)
JDK描述
public final class Integer
extends Number
implements Comparable
Integer 類在對象中包裝了一個基本類型 int 的值。Integer 類型的對象包含一個 int 類型的字段。
此外,該類提供了多個方法,能在 int 類型和 String 類型之間互相轉換,還提供了處理 int 類型時非常有用的其他一些常量和方法。
public final class Long
extends Number
implements Comparable
Long 類在對象中包裝了基本類型 long 的值。每個 Long 類型的對象都包含一個 long 類型的字段。
此外,該類提供了多個方法,可以將 long 轉換為 String,將 String 轉換為 long,除此之外,還提供了其他一些處理 long 時有用的常量和方法。
代碼分析
Integer和Long類比較類似,放在一起看了
繼承了Number類,實現了一個接口
- Number是Integer和Long的父類,其實現了Serializable接口,支持序列化;在java中,很多數字類型的類都是Number類的子類,如常見的Byte、Double、Float、Long、Short,還有AtomicInteger、AtomicLong、BigDecimal等。
Number類需要實現下列方法,便於類型轉換
public abstract int intValue();
public abstract long longValue();
public abstract float floatValue();
public abstract double doubleValue();
public byte byteValue()
public short shortValue()
- Comparable接口,實現compareTo方法,實現比較
變量
- private final int value; 保存值,Integer是有符號的,0為正,1為負數
- public static final int MIN_VALUE = 0x80000000; 最小值 1000...0000 -2^31
- public static final int MAX_VALUE = 0x7fffffff; 最大值 0111...1111 2^31-1
- public static final Class
TYPE;類型Class - final static char[] digits ;保存所有可展現數字的char,包括0-9 a-z
- public static final long MIN_VALUE = 0x8000000000000000L;最小值 -263
- public static final long MAX_VALUE = 0x7fffffffffffffffL;最大值 263-1
- public static final Class
TYPE;Long的類型Class
構造方法
- public Integer(int value) ;this.value = value;
- public Integer(String s) ;this.value = parseInt(s, 10);
其他方法
轉字符串
- public static String toString(int i, int radix);根據進制返回字符串,進制范圍為2-36
- public static String toHexString(int i) ;返回16進制字符串,toUnsignedString的shift為4
- public static String toOctalString(int i);返回8進制字符串,toUnsignedString的shift為3
- public static String toBinaryString(int i) ;返回2進制字符串,toUnsignedString的shift為1
- private static String toUnsignedString(int i, int shift);計算mask后從digits中取字符
- public static String toString(int i) 返回字符串
其他對象轉Integer
- public static int parseInt(String s, int radix);將字符串轉為int
- public static int parseInt(String s);默認10進制
- public static Integer valueOf(String s, int radix)
- public static Integer valueOf(String s)
- public static Integer valueOf(int i);先從cache中取,若無new Integer
轉其他類型,均為強轉
- public byte byteValue()
- public short shortValue()
- public int intValue()
- public long longValue()
- public float floatValue()
- public double doubleValue()
其他方法
- public int hashCode() 返回value
- public boolean equals(Object obj) obj必須是Integer;i.equals(23) 23會被裝箱為Integer
- public static Integer getInteger(String nm);從System.getProperty中獲取Integer的值
- public static Integer decode(String nm) ;將nm解碼為Integer,nm
- public int compareTo(Integer anotherInteger);比較
- public static int compare(int x, int y);比較
decode(String nm)可解碼的字符串
Signopt 0x HexDigits
Signopt 0X HexDigits
Signopt # HexDigits
Signopt 0 OctalDigits
其他
- int i=3;int到Integer會調用Integer.valueOf(3)方法,將-128~127緩存到IntegerCache,緩存的原因是java規范這樣規定了
相關問題
觀察下面的代碼
Integer k=new Integer(23);
Integer j=Integer.valueOf("23");
System.out.println(k==j);//返回false
Integer i=23;
Integer j=Integer.valueOf("23");
System.out.println(i==j);//返回true,緩存了
Integer i=223;
Integer j=Integer.valueOf("223");
System.out.println(i==j);//返回false,未緩存
總結
- System.getProperty http://www.cnblogs.com/ssj234/p/6389385.html
- highestOneBit