bl双性强迫侵犯h_国产在线观看人成激情视频_蜜芽188_被诱拐的少孩全彩啪啪漫画

Java基礎之Integer使用的注意事項是什么-創(chuàng)新互聯(lián)

這篇文章主要介紹了Java基礎之Integer使用的注意事項是什么,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

成都網(wǎng)站設計、做網(wǎng)站、成都外貿(mào)網(wǎng)站建設公司的關注點不是能為您做些什么網(wǎng)站,而是怎么做網(wǎng)站,有沒有做好網(wǎng)站,給創(chuàng)新互聯(lián)建站一個展示的機會來證明自己,這并不會花費您太多時間,或許會給您帶來新的靈感和驚喜。面向用戶友好,注重用戶體驗,一切以用戶為中心。

JAVA中Integer對象的引用

JAVA中沒有指針一說,但也有引用的概念。這里要說的主要是Integer是不是同一個對象。

1、先看一段代碼:

public static void main(String[] args){ 
Integer a1 = 100; 
Integer b1 = a1;//另一種也可以b1=100 
Field field = null; 
try { 
field = a1.getClass().getDeclaredField("value"); 
} catch (NoSuchFieldException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} catch (SecurityException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} 

field.setAccessible(true); 

try { 
field.set(a1, 5000); 
} catch (IllegalArgumentException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} catch (IllegalAccessException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} 
System.out.println("b1="+b1); 

Integer c1 = 100; 
System.out.println("c1="+c1); 

}

結果:

b1=5000

c1=5000

從上面,首先這里要說明幾個,

1)、對于Integer來說,-128-127之間的整型已經(jīng)初始化放在IntegerCache中,如果是裝箱的話,就會從這里面取對象。

2)、b1=a1到底是數(shù)字賦值還是同一個對象?這個從結果實際就可以看出來,b1和a1指向同一個對象,而不是同一個數(shù)值

3)、c1=100,說明對于-128-127之間的數(shù)值,都是從IntegerCache中獲取的對象,100對應的Integer對象被改變后,后續(xù)對于100的裝箱都被改變。因為獲取cache中對象時用的是數(shù)組索引,而不是數(shù)值比較獲取的。

不過修改這個緩存會比較危險,不介意。誰知道什么jar包或者什么平臺來個100的裝箱,但得到結果又不是100,到時就崩潰了。

2、通過上面描述,那么如果改成這樣又是什么答案

public static void main(String[] args){ 
Integer a1 = 200; 
Integer b1 = a1; 
Field field = null; 
try { 
field = a1.getClass().getDeclaredField("value"); 
} catch (NoSuchFieldException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} catch (SecurityException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} 

field.setAccessible(true); 

try { 
field.set(a1, 5000); 
} catch (IllegalArgumentException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} catch (IllegalAccessException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} 
System.out.println("b1="+b1); 

Integer c1 = 200; 
System.out.println("c1="+c1); 

}

3、那么再改一下

public static void main(String[] args){ 
Integer a1 = new Integer(100); 
Integer b1 = a1; 
Field field = null; 
try { 
field = a1.getClass().getDeclaredField("value"); 
} catch (NoSuchFieldException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} catch (SecurityException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} 

field.setAccessible(true); 

try { 
field.set(a1, 5000); 
} catch (IllegalArgumentException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} catch (IllegalAccessException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} 
System.out.println("b1="+b1); 

Integer c1 = 100; 
System.out.println("c1="+c1); 

}

這又是什么答案。對于new的操作,是不進行裝箱的,而是在堆中生成對象的。

理解了裝箱、緩存、引用就不難理解了。可以自己試試。

先來點基礎的知識

   基本類型和包裝類的對應 
  byte  Byte 
  short  Short 
  int  Integer 
  long  Long 
  float  Float 
  double  Double 
  char  Character 
  boolean  Boolean

上述的八中基本數(shù)據(jù)類型的對應關系只有 int->Integer    char->Character    兩個變化較大,其余都只是將首字母轉換為小寫。
再來了解一下JDK5的新特性:自動裝箱和拆箱

自動裝箱:把基本類型轉換為包裝類類型

自動拆箱:把包裝類類型轉換為基本類型

public class Demo_Integer { 
 public static void main(String[] args) { 
//JDK1.5之前 
 int a = 100; 
 Integer a1 = new Integer(a); //將基本數(shù)據(jù)類型包裝成對象,裝箱 
 int b = a1.intValue(); //將對象轉換為基本數(shù)據(jù)類型,拆箱 
//JDK1.5之后 
 int x = 100; 
 Integer x1 = x;  //自動裝箱,把基本數(shù)據(jù)類型轉換為對象 
 int y = x1 + x;  //自動拆箱,把對象轉換為基本數(shù)據(jù)類型 
 } 
}

注意事項

public class Demo_Integer { 
 public static void main(String[] args) { 
  Integer a = null; 
  int b = a + 100; //自動拆箱底層將會調用a.intValue(),a為null,自然會拋出 NullPointerException 
  System.out.println(b); 
 } 
}

面試題

public class Demo_Integer { 
 public static void main(String[] args) { 
  Integer i1 = new Integer(97); 
  Integer i2 = new Integer(97); 
  System.out.println(i1 == i2); 
  System.out.println(i1.equals(i2)); 
  System.out.println("-----------"); 
  
  Integer i3 = new Integer(197); 
  Integer i4 = new Integer(197); 
  System.out.println(i3 == i4); 
  System.out.println(i3.equals(i4)); 
  System.out.println("-----------"); 
 } 
}
Output: 
 false 
 true 
 ----------- 
 false 
 true 
 -----------

原因:  

    new 是在堆內(nèi)存開辟空間的,自然比較地址值(==)都為false.  

    由于Integer重寫了equals方法,所以equals輸出都為true.

你可能感覺太簡單了,沒有任何技術含量,因為上面的不是重點,看下面代碼

public class Demo_Integer { 
 public static void main(String[] args) { 
  Integer i1 = 127; 
  Integer i2 = 127; 
  System.out.println(i1 == i2); 
  System.out.println(i1.equals(i2)); 
  System.out.println("-----------"); 
 
  Integer i3 = 128; 
  Integer i4 = 128; 
  System.out.println(i3 == i4); 
  System.out.println(i3.equals(i4)); 
  System.out.println("-----------"); 
 } 
}
Output: 
true 
true 
----------- 
false 
true 
-----------

原因:  

    為什么當int大于127就是兩個對象,127這個數(shù)字是不是覺得很熟悉?  

    -128到127是byte的取值范圍,如果在這個取值范圍內(nèi),自動裝箱就不會創(chuàng)建新對象了,而從常量池中獲取  

    超過了byte的取值范圍就會在創(chuàng)建新對象

自動裝箱其底層會調用valueOf()方法,簡單源碼分析(JDK1.8):

public final class Integer extends Number implements Comparable<Integer> { 
 public static Integer valueOf(int i) { 
  //當 i >= -128 且 i <= 127 時,會直接將取緩沖區(qū)中的對象 
  if (i >= IntegerCache.low && i <= IntegerCache.high) 
   return IntegerCache.cache[i + (-IntegerCache.low)]; 
  return new Integer(i);//超過了byte取值范圍會在堆內(nèi)存創(chuàng)建 
 } 
 
 //內(nèi)部類充當緩沖區(qū) 
 private static class IntegerCache { 
  static final int low = -128; 
  static final int high; 
  static final Integer cache[]; 
 
  static { 
   // high value may be configured by property 
   int h = 127; 
   String integerCacheHighPropValue = 
    sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high"); 
   if (integerCacheHighPropValue != null) { 
    try { 
     int i = parseInt(integerCacheHighPropValue); 
     i = Math.max(i, 127); 
     // Maximum array size is Integer.MAX_VALUE 
     h = Math.min(i, Integer.MAX_VALUE - (-low) -1); 
    } catch( NumberFormatException nfe) { 
     // If the property cannot be parsed into an int, ignore it. 
    } 
   } 
   high = h; 
 
   cache = new Integer[(high - low) + 1]; 
   int j = low; 
   for(int k = 0; k < cache.length; k++) 
    cache[k] = new Integer(j++); 
 
   // range [-128, 127] must be interned (JLS7 5.1.7) 
   assert IntegerCache.high >= 127; 
  } 
   private IntegerCache() {} 
 } 
  
}

8種基本類型的包裝類和對象池

java中基本類型的包裝類的大部分都實現(xiàn)了常量池技術,這些類是Byte,Short,Integer,Long,Character,Boolean,另外兩種浮點數(shù)類型的包裝類則沒有實現(xiàn)。另外Byte,Short,Integer,Long,Character這5種整型的包裝類也只是在對應值小于等于127時才可使用對象池,也即對象不負責創(chuàng)建和管理大于127的這些類的對象

擴展知識

在jvm規(guī)范中,每個類型都有自己的常量池。常量池是某類型所用常量的一個有序集合,包括直接常量(基本類型,String)和對其他類型、字段、方法的符號引用。之所以是符號引用而不是像c語言那樣,編譯時直接指定其他類型,是因為java是動態(tài)綁定的,只有在運行時根據(jù)某些規(guī)則才能確定具體依賴的類型實例,這正是java實現(xiàn)多態(tài)的基礎。

在JVM中,類從被加載到虛擬機內(nèi)存中開始,到卸載出內(nèi)存為止,它的整個生命周期包括:加載、驗證、準備、解析、初始化、使用和卸載7個階段。而解析階段即是虛擬機將常量池內(nèi)的符號引用替換為直接引用的過程。

感謝你能夠認真閱讀完這篇文章,希望小編分享的“Java基礎之Integer使用的注意事項是什么”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關知識等著你來學習!

當前文章:Java基礎之Integer使用的注意事項是什么-創(chuàng)新互聯(lián)
文章轉載:http://vcdvsql.cn/article22/ichjc.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作微信小程序小程序開發(fā)網(wǎng)站維護網(wǎng)站導航網(wǎng)站建設

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

綿陽服務器托管