<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                >[success] # Integer 1. **Integer**作為 **int**包裝類,包裝了一個**int類型的變量**作為成員變量,主要用于實現對**int類型的包裝**并提供**int類型到String類之間的轉換**等方法 * 進入**Integer 類**中可以看到內部有一個常量 這個常量int 就是被包裝的int 并用其值提供了一些方法 ![](https://img.kancloud.cn/35/27/35271d3815c48250a02b6704635dbd68_632x189.png) * 創建時候構造函數將傳入基本變量賦值個了value 常量 ![](https://img.kancloud.cn/8a/ac/8aaca910a83233802c565bb151abe23b_516x139.png) >[danger] ##### 常用的常量 |常量類型和名稱 |功能介紹| | --- | --- | |public static final int MAX_VALUE |表示int類型可以描述的最大值,即2^31-1| |public static final int MIN_VALUE| 表示int類型可以描述的最小值,即-2^31| |public static final int SIZE |表示int類型采用二進制補碼形式的位數| |public static final int BYTES |表示int類型所占的字節個數| |public static final Class TYPE| 表示int類型的Class實例| ~~~ public class JavaTest { public static void main(String[] args) { // 類方法直接調 System.out.println("最大值是:" + Integer.MAX_VALUE); // 2^31-1 System.out.println("最小值是:" + Integer.MIN_VALUE); // -2^31 System.out.println("所表示二進制的位數是:" + Integer.SIZE); // 32 System.out.println("所占字節的個數是:" + Integer.BYTES); // 4 System.out.println("對應int類型的Class實例是:" + Integer.TYPE); // int } } ~~~ >[danger] ##### 常用的方法 |方法聲明 | 功能介紹 | | --- | --- | |Integer(int value) | 根據參數指定的整數來構造對象(已過時) | |Integer(String s) |根據參數指定的字符串來構造對象 (已過時) | |int intValue() |獲取調用對象中的整數值并返回 | |static Integer valueOf(int i) |根據參數指定整數值得到Integer類型對象 | |boolean equals(Object obj) |比較調用對象與參數指定的對象是否相等 | |String toString() |返回描述調用對象數值的字符串形式 | |static int parseInt(String s) |將字符串類型轉換為int類型并返回 | |static String toString(int i) |獲取參數指定整數的十進制字符串形式 | |static String toBinaryString(int i) |獲取參數指定整數的二進制字符串形式 | |static String toHexString(int i) |獲取參數指定整數的十六進制字符串形式 | |static String toOctalString(int i) |獲取參數指定整數的八進制字符串形式 | * 說明 通過**構造函數**方式創建 一個int 包裝類已經過時,更多推薦使用**valueOf**創建 ![](https://img.kancloud.cn/33/c9/33c9596e1a552c2017f6a6dd171bd7c0_621x360.png) * 案例 ~~~ package javaTest; public class JavaTest { public static void main(String[] args) { // 推薦使用valueOf Integer it3 = Integer.valueOf(1); Integer it4 = Integer.valueOf("1"); // 通過 intValue 拆包獲取int int num = it4.intValue(); System.out.println(it3); // 1 字符串類型 System.out.println 會調用類的toString 方法 等同于it3.toString() System.out.println(num); // 1 數字類型 打印就是基本類型數字1 // --------------128 ------------------------ // Integer類內部 通過靜態內部類提供了一個緩存池,范圍在-128~127之間,如果超過這個范圍 Integer 值都是new出來的新對象 System.out.println(it3 == it4); // true 對象比較是內存地址但是是-128~127之間 因此二者使用是一個指向 System.out.println(it3.equals(it4)); // true 比較內部值是否相同使用equals Integer it5 = Integer.valueOf(128); Integer it6 = Integer.valueOf("128"); System.out.println(it5 == it6); // false 對象比較是內存地址 不同 是-128~127之間 創建了兩個新對象 System.out.println(it5.equals(it6)); // true 比較內部值是否相同使用equals // 類型轉換 int ic = Integer.parseInt("200"); //int ic = Integer.parseInt("200a"); // 編譯ok,運行發生NumberFormatException數字格式異常,因為有字母 System.out.println("字符串轉換為整數的結果是:" + ic); // 200 System.out.println("根據參數指定的整數獲取對應的十進制字符串是:" + Integer.toString(200)); System.out.println("根據參數指定的整數獲取對應的二進制字符串是:" + Integer.toBinaryString(200)); System.out.println("根據參數指定的整數獲取對應的十六進制字符串是:" + Integer.toHexString(200)); System.out.println("根據參數指定的整數獲取對應的八進制字符串是:" + Integer.toOctalString(200)); } } ~~~ * Integer類內部 通過靜態內部類提供了一個緩存池,范圍在-128~127之間,如果超過這個范圍 Integer 值都是new出來的新對象 ![](https://img.kancloud.cn/b7/f9/b7f9fa00977bed9e477fc98e70970d5d_590x150.png) >[info] ## 裝箱和拆箱 1. 自動裝箱: 基本數據類型->包裝類。對應Integer.valueOf(int i); 2. 自動拆箱:包裝類->數據類型。對應Integer.intValue(); ~~~ public class JavaTest { public static void main(String[] args) { // 自動裝箱: 基本數據類型->包裝類。對應Integer.valueOf(int i); // 自動拆箱:包裝類->數據類型。對應Integer.intValue(); Integer it5 = 100; // 直接通過賦值運算符實現自動裝箱 int ib = it5; // 直接通過賦值運算符實現自動拆箱 // 裝箱案例 Integer[] it = new Integer[5]; it[0] = 1; // 雖然接受的是Integer 類型 但是賦值int 類型時候進行了裝箱 // 拆箱案例 Integer it1 = 100; int num = 100; System.out.println(it1 == num); // true 因為做了拆箱實際 運行 it1.intValue()==num /** * 下面代碼實際 運行效果 * Integer i = Integer.valueOf(50);//java自動裝包 * Integer j = Integer.valueOf(60);//java 自動裝包 * System.out.println(i.intValue()+i.intValue());//java 自動拆包 */ Integer i = 50; Integer j = 60; System.out.println(i + j); // 注意 -128 - 127 問題,當Integer i1 = 50 執行時候進行了裝箱 // 進行了裝箱 執行了Integer.valueOf(50); 因為valueOf 會命中緩存 // 所以 i1 == i2 為true ,i1 == j2 i1會進行拆包,把值轉為int類型 因此是true // k3 通過構造創建的因此沒有緩存說法 因此i1 == k3 為false Integer i1 = 50; Integer i2 = 50; int j2 = 50; Integer k3 = new Integer(50); System.out.println(i1 == j2);// true System.out.println(i1 == i2);// true System.out.println(i1 == k3);// false } } ~~~ >
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看