一道有意思的題目之Integer

  
今天在公衆號上看到一道有點意思的題目:
Integer a = 127;
Integer b = 127;
Integer e = 128;
Integer f = 128;

System.out.println(a == b);
System.out.println(e == f);

 很基礎的東西,時間久了也容易忘記,下午抽空回憶了一下:
public static voidmain(String[] args) {
    Integer a =
127;
   
Integer b = 127;
   
Integer e = 128;
   
Integer f = 128;// new Integer("128")new Integer(128)

   
System.out.println(a == b);// true  -128~127之間的數是緩衝區取的,所以用==比較爲true,超過該範圍則爲false
   
System.out.println(e == f);// false   Double 沒有緩衝區
   
System.out.println(e == 128);// true
   
System.out.println(f == 128);// true
   
System.out.println(e.intValue() == f.intValue());// true
   
System.out.println(e.equals(f));// true

   
inti_max = Integer.MAX_VALUE;//大於2147483647;則編譯報錯integer num too large
   
inti_min = Integer.MIN_VALUE;//小於-2147483648;編譯報錯
   
//System.out.println(i_max.equals(i_min));//編譯報錯 基本類型不是對象,不能用equals

   
inti = Integer.parseInt("123");//將有符號的十進制整數字符串
   
    Integer ii = Integer.valueOf(123);//將指定的整數字符串轉換成Integer對象
  
    Integer iii = Integer.getInteger("123");//根據制定的名稱得到系統屬性的整數值
   
    System.out.println(iii);// null
   
iii =Integer.parseInt("123",10);
   
System.out.println(iii);// 123
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章