【Java SE】包裝類 Byte、Short、Integer、Long、Character、Float、Double、Boolean

參考筆記:java 包裝類 萬字詳解(通俗易懂)_java包裝類-CSDN博客

目錄

1.簡介

2.包裝類的繼承關系圖

3.裝箱和拆箱

3.1 介紹

3.2 手動拆裝箱

3.3. 自動拆裝箱

?4.關于String類型的轉化問題

4.1 String類型和基本類型的相互轉化

4.1.1 String ——> 基本類型

?4.1.2 基本類型?——> String

4.2 String類型和包裝類型的相互轉化

4.2.1 String ——> 包裝類

4.2.2 包裝類 ——> String

5.八大包裝類的常用成員方法

?5.1 Byte類常用方法

5.2 Short類常用方法

5.3 Integer類常用方法

5.4 Long類常用方法

5.5 Character類常用方法?

5.6 Float類常用方法

5.7 Double類常用方法

5.8 Boolean類常用方法

6.Integer創建機制的面試題(重要)

6.1 練習題1

6.2 練習題2


1.簡介

基本數據類型不是對象,不能使用類的方法;因此,? Java 針對基本類型提供了它們對應的包裝類,八大基本數據類型,對應了八種包裝類,以對象的形式來調用。包裝類有了類的特點,使得可以調用包裝類中的方法

八大包裝類位于 java.lang 包下,如下圖所示:

?

除了 IntegerCharacter 這兩個包裝類外,其他六個包裝類的類名均是對應的基本類型首字母大寫后得到的

2.包裝類的繼承關系圖

藍色實線:繼承extends

綠色虛線:實現implements

Byte、Integer、Long、Float、Double、Short 的繼續關系圖

?Boolean 的繼承關系圖

?Character 的繼承關系圖

3.裝箱和拆箱

3.1 介紹

  • 裝箱:基本類型?——> 包裝類型(或者叫對象類型,引用類型)

  • 拆箱:包裝類型 ——> 基本類型

3.2 手動拆裝箱

JDK5.0 之前,拆裝箱均是程序員手動完成的

  • 手動裝箱:可以使用包裝類的構造器完成,也可以使用 valueOf() 方法

  • 手動拆箱:Integer 類為例,需要用到 intValue() 方法;以 Character 類為例,需要用到 charValue() 方法

案例1:Integer包裝類的手動拆裝箱過程?

public class demo {public static void main(String[] args) {//JDK5.0之前,拆裝箱都是手動完成int temp = 19;//手動裝箱(基本類型 ————> 包裝/引用類型)Integer integer_0 = new Integer(temp);//使用包裝類的構造器完成裝箱Integer integer_1 = Integer.valueOf(temp);//使用包裝類的valueOf方法完成裝箱//手動拆箱(包裝/引用類型 ————> 基本類型)int tempI_0 = integer_0.intValue();//使用包裝類的intValue方法完成拆箱System.out.println("integer_0的值 = " + integer_0);System.out.println("integer_1的值 = " + integer_1);System.out.println("tempI_0 = " + tempI_0);}
}

?案例1運行結果

案例2:Boolean包裝類的手動拆裝箱過程?

public class demo {public static void main(String[] args) {//JDK5.0之前,拆裝箱都是手動完成char temp = 'a';//手動裝箱(基本類型 ————> 包裝/引用類型)Character character_1 = new Character(temp);//使用包裝類的構造器完成裝箱Character character_2 = Character.valueOf(temp);//使用包裝類的valueOf方法完成裝箱//手動拆箱(包裝/引用類型 ————> 基本類型)char tempI_0 = character_1.charValue();//使用包裝類的charValue方法完成拆箱System.out.println("character_1的值 = " + character_1);System.out.println("character_2的值 = " + character_2);System.out.println("tempI_0 = " + tempI_0);}
}

?案例2的執行結果

?

3.3. 自動拆裝箱

JDK5.0 開始,Java 提供了自動拆裝箱的機制(不需要手動調用構造器或者方法了)

  • 自動裝箱:實際上底層調用了 valueOf() 方法

  • 手動拆箱:實際上底層調用了 intValue()方法(以Integer包裝類為例)

案例:Integer包裝類的自動拆裝箱?

public class demo {public static void main(String[] args) {//JDK5.0以后,java提供了自動拆裝箱Integer integer_1 = 199;    //(自動)裝箱——其實底層調用的仍然是valueOf方法(可Debug)int tempI_1 = integer_1;    //(自動)拆箱——其實底層調用的仍然是intValue方法System.out.println("integer_1的值 = " + integer_1);System.out.println("tempI_1 = " + tempI_1);System.out.println("----------------------------------");}
}

運行結果

Debug驗證自動拆裝箱是否在底層調用了手動拆裝箱時用到的方法:valueOf、intValue

由上面的 GIF 可以看到,自動裝拆箱底層分別調用了valueOf() 方法、intValue()方法

另外,關于 IntegervalueOf() 方法源碼如下 :

 @IntrinsicCandidatepublic static Integer valueOf(int i) {if (i >= IntegerCache.low && i <= IntegerCache.high)return IntegerCache.cache[i + (-IntegerCache.low)];return new Integer(i);}

源碼的 valueOf 方法中有一個 if 條件語句的判斷,它的意思是:如果傳入的 int 基本類型的值在這個范圍內,就不 new 新的 Integer 對象,而是調用底層的用 static final 修飾的 Integer 緩沖數組 cache。通過追溯源碼,可以得知這里的?lowhigh 的實際范圍是 -128 ~ 127,如下圖所示 :?

緩沖數組 cache 中存放的元素是[-128,127]。可以通過 Debug 來看看底層的緩沖數組是否真實存在,如下GIF所示?:?

?4.關于String類型的轉化問題

4.1 String類型和基本類型的相互轉化

4.1.1 String ——> 基本類型

包裝類中的 parseXxx 方法可以將字符串類型的數據轉換成對應的基本類型,需要用相應的基本類型來作接收

需要注意的是,在將字符串類型轉為其他基本類型前,一定要確認字符串里面的內容能否正常轉換,比方說,如果想把 "唱跳rap籃球"?這段字符串轉換為 int 類型,這時候一定會發生數字格式異常,如下圖所示 :

String ?——> 基本類型演示(byte、short、int、long、float、double、boolean)

public class demo {public static void main(String[] args) {//parseXxx(String),以對應的基本類型作接收byte temp_byte = Byte.parseByte("11");short temp_short = Short.parseShort("141");int temp_int = Integer.parseInt("430");long temp_long = Long.parseLong("11211");float temp_float = Float.parseFloat("66.66F");double temp_double = Double.parseDouble("666.666");boolean temp_boolean = Boolean.parseBoolean("true");System.out.println("temp_byte = " + temp_byte);System.out.println("temp_short = " + temp_short);System.out.println("temp_int = " + temp_int);System.out.println("temp_long = " + temp_long);System.out.println("temp_float = " + temp_float);System.out.println("temp_double = " + temp_double);System.out.println("temp_boolean = " + temp_boolean);}
}

運行結果:

?String ?——> char 演示

在八大包裝類中,除了 Character 類外,其他的 7 種包裝類中都有 parseXxx 方法。如果想將 String 字符串類型的數據轉換成 char 類型的數據,可以通過 String 類中的兩個方法來完成:

① char[] toCharArray() : 將字符串轉換成字符數組
② char charAt(int index) : 獲取指定索引位置的字符

public class demo {public static void main(String[] args) {//定義一個字符串String str = "蔡徐坤";//利用toCharArray() 方法將字符串轉換為字符數組char[] charArray = str.toCharArray();System.out.println("string字符串一共有" + charArray.length + "個字符.");for (int i = 0; i < charArray.length; i++) {System.out.println("第" + (i + 1) + "個字符是:" + charArray[i]);}System.out.println("---------------------------------------");//利用charAt方法來直接獲取字符串中的每一個字符元素char temp_char_0 = str.charAt(0);char temp_char_1 = str.charAt(1);char temp_char_2 = str.charAt(2);System.out.println("string字符串第一個元素為:" + temp_char_0);System.out.println("string字符串第二個元素為:" + temp_char_1);System.out.println("string字符串第三個元素為:" + temp_char_2);}
}

運行結果:

?4.1.2 基本類型?——> String

基本類型轉 String 最常見的兩種方式:

① 直接與空字符串進行拼接

② String類的valueOf方法?

案例?

public class demo {public static void main(String[] args) {//方法一 : 以空字符串拼接的形式//byte --> Stringbyte temp_byte = 127;String temp_string_0 = 127 + "";//short --> Stringshort temp_short = 141;String temp_string_1 = temp_short + "";//int --> Stringint temp_int = 428;String temp_string_2 = temp_int + "";//long --> Stringlong temp_long = 11211;String temp_string_3 = temp_long + "";//float --> Stringfloat temp_float = 135.0F;String temp_string_4 = temp_float + "";//double --> Stringdouble temp_double = 433.0;String temp_string_5 = temp_double + "";//char --> Stringchar temp_char = 'A';String temp_string_6 = temp_char + "";//boolean --> Stringboolean temp_boolean = true;String temp_string_7 = temp_boolean + "";System.out.println("temp_string_0 = " + temp_string_0);System.out.println("temp_string_1 = " + temp_string_1);System.out.println("temp_string_2 = " + temp_string_2);System.out.println("temp_string_3 = " + temp_string_3);System.out.println("temp_string_4 = " + temp_string_4);System.out.println("temp_string_5 = " + temp_string_5);System.out.println("temp_string_6 = " + temp_string_6);System.out.println("temp_string_7 = " + temp_string_7);System.out.println("========================================");//方法二 : 利用String類的valueOf方法temp_string_0 = String.valueOf(temp_byte) + "_EX";temp_string_1 = String.valueOf(temp_short) + "_EX";temp_string_2 = String.valueOf(temp_int) + "_EX";temp_string_3 = String.valueOf(temp_long) + "_EX";temp_string_4 = String.valueOf(temp_float) + "_EX";temp_string_5 = String.valueOf(temp_double) + "_EX";temp_string_6 = String.valueOf(temp_char) + "_EX";temp_string_7 = String.valueOf(temp_boolean) + "_EX";System.out.println("temp_string_0 = " + temp_string_0);System.out.println("temp_string_1 = " + temp_string_1);System.out.println("temp_string_2 = " + temp_string_2);System.out.println("temp_string_3 = " + temp_string_3);System.out.println("temp_string_4 = " + temp_string_4);System.out.println("temp_string_5 = " + temp_string_5);System.out.println("temp_string_6 = " + temp_string_6);System.out.println("temp_string_7 = " + temp_string_7);}
}

4.2 String類型和包裝類型的相互轉化

4.2.1 String ——> 包裝類

String 類轉化為包裝類有 2 種方式:

① 利用包裝類的parseXXX例如:Integer integer_0 = Integer. parseInt(字符串類型變量);

② 利用包裝類的構造器,例如 : Integer integer_1 = new Integer(字符串類型變量);?

補充:方式?①?中用到了 parseInt 方法,而在上文 String 類型轉基本類型時也用到了 parseInt 方法,但當時是用 int 類型變量來作接收的。先看一下 parseInt 的源碼:

可以看到 parseInt 的返回值類型是 int 類型,因此方式?實際上應用了自動裝箱,把等號右邊返回的 int 類型的值,在底層又調用 valueOf 方法裝箱成了 Integer 包裝類

案例?

public class demo {public static void main(String[] args) {//演示 : String類型 ————> 包裝類型//方式一String string_0 = "141";Integer integer_0 = Integer.parseInt(string_0);//方式二String string_1 = "133";Integer integer_1 = new Integer(string_1);System.out.println("integer_0的值 = " + integer_0);System.out.println("integer_1的值 = " + integer_1);}
}

4.2.2 包裝類 ——> String

包裝類轉換為 String 類型有 3 種方式:

String xxx = 包裝類變量名 + "";
String xxx = 包裝類類名.toString();
String xxx = String.valueOf(...);

方式 和上文中提到的基本類型轉 String 類型的方式是一回事

方式?體現出包裝類相對于基本類型的優勢,可以直接調用包裝類中的方法

方式?③?使用的是 StringvalueOf 方法,valueOf 內部調用的就是包裝類的 toString 方法,源碼如下:

案例:Integer ——> String

public class demo{public static void main(String[] args){Integer integer_0 = 146;//自動裝箱//包裝類->String類的三種方式String str1 = integer_0 + "";//方式1String str2 = Integer.toString(integer_0);//方式2String str3 = String.valueOf(integer_0);//方式3System.out.println("str1 = " + str1);System.out.println("str2 = " + str2);System.out.println("str3 = " + str3);}
}

5.八大包裝類的常用成員方法

每個包裝類下的方法都很多,本文只講一些比較常用的,遇到不懂的問問 GPT 或者 DeepSeek 就行了。查看其方法可以通過如下步驟:

?5.1 Byte類常用方法

byte byteValue() :返回當前 Byte 類對象對應的值,以 byte 類型作接收

②?static int compare(byte x, byte y) :比較兩個 byte 變量的值。返回值:前面 byte 變量的值減去后面 byte 變量的值

③?int compareTo(Byte anotherByte):比較兩個 Byte 類對象的值,返回值同?

④?double doubleValue() :返回當前 Byte 類對象對應的值,以 double 類型作接收

int intValue():返回當前 Byte 類對象對應的值,以 int 類型作接收

⑥ static int parseByte(String xxx):?字符串類型 ——> byte 類型

String toString():將當前 Byte 對象的值轉換為 String 類型

static String toString(byte b):將指定的 byte 值轉換為 String 對象

static Byte valueOf(...):字符串類型 ——> Byte 類型

案例演示

public class demo {public static void main(String[] args) {//演示 : Byte類常用方法//1 —— byte byteValue() : 返回當前Byte類對象對應的值,以byte類型作接收Byte b = 127;   //自動裝箱byte bb = b.byteValue();System.out.println("byte類型變量bb = " + bb);System.out.println("----------------------------------");//2 —— static int compare(byte x, byte y) : 比較兩個byte變量的值, 返回值為前面byte變量的值減去后面byte變量的值byte temp_b_0 = 5;byte temp_b_1 = 1;int i = Byte.compare(temp_b_0, temp_b_1);System.out.println("temp_b_0 - temp_b_1 = " + i);System.out.println("----------------------------------");//3 —— int compareTo(Byte anotherByte) : 比較兩個Byte類對象的值,返回值同方法2Byte temp_B_0 = 55;Byte temp_B_1 = 11;int i1 = temp_B_0.compareTo(temp_B_1);System.out.println("temp_B_0 - temp_B_1 = " + i1);System.out.println("----------------------------------");//4 —— double doubleValue() : 返回當前Byte類對象對應的值,以double類型作接收double bb1 = b.doubleValue();System.out.println("double類型變量bb1 = " + bb1);System.out.println("----------------------------------");//5 —— int intValue() : 返回當前Byte類對象對應的值,以int類型作接收int bb2 = b.intValue();System.out.println("int類型變量bb2 = " + bb2);System.out.println("----------------------------------");//6 —— static int parseByte(String xxx) : 字符串類型 ——> byte類型byte temp_b_2 = Byte.parseByte("1");System.out.println("byte類型變量temp_b_2 = " + temp_b_2);System.out.println("----------------------------------");//7 —— String toString() : 將當前Byte對象的值轉換為String類型Byte temp_B_2 = 127;String string_0 = temp_B_2.toString();System.out.println("Byte類型對象temp_B_2的字符串形式為:" + string_0);System.out.println("----------------------------------");//8 —— static String toString(byte b) : 將指定的byte值轉換為String對象byte temp_b_3 = 2;String string_1 = Byte.toString(temp_b_3);System.out.println("byte類型變量temp_b_3的字符串形式為:" + string_1);System.out.println("----------------------------------");//9 —— static Byte valueOf(...) : 字符串類型 ——> Byte類型Byte temp_B_3 = Byte.valueOf("11");System.out.println("Byte類型對象temp_B_3的值 = " + temp_B_3);}
}

5.2 Short類常用方法

① Short.MIN_VALUE、Short.MAX_VALUE:返回 Short/short 類型的最小值、最大值

② short shortValue():返回當前 Short 類對象對應的值,以 short 類型作接收

③?static int compare(short?x, short?y):比較兩個 short 變量的值, 返回值:前面 short 變量的值減去后面 short 變量的值

④?int compareTo(Short anotherShort):比較兩個 Short 類對象的值,返回值同

⑤?double doubleValue():返回當前 Short 類對象對應的值,以 double 類型作接收

⑥ int intValue():返回當前 Short 類對象對應的值,以 int 類型作接收

⑦ static int parseShort(String xxx) : 字符串類型 ——>? short 類型

⑧ String toString():將當前 Short 對象的值轉換為 String 類型

⑨ static String toString(short s):將指定的 short 值轉換為 String 對象

⑩ static Short valueOf(...):字符串類型 ——> Short 類型

案例演示

public class demo {public static void main(String[] args) {//演示Short類常用方法//1 —— Short.MIN_VALUE,Short.MAX_VALUE,返回Short/short類型的最小值、最大值System.out.println(Short.MIN_VALUE);System.out.println(Short.MAX_VALUE);System.out.println("------------------------------------");//2 —— short shortValue() : 返回當前Short對象的值,以short基本類型作接收Short temp_S_0 = 128;       //自動裝箱short temp_s_0 = temp_S_0.shortValue();System.out.println("short類型變量temp_s_0 = " + temp_s_0);System.out.println("------------------------------------");//3 —— static int compare(short x, short y) : 比較兩個short變量的值, 返回值為前面short變量的值減去后面short變量的值short temp_s_1 = 6;short temp_s_2 = 3;int i = Short.compare(temp_s_1, temp_s_2);System.out.println("temp_s_1 - temp_s_2 = " + i);System.out.println("------------------------------------");//4 —— int compareTo(Short anotherShort) : 比較兩個Short類對象的值,返回值同3Short temp_S_1 = 66;Short temp_S_2 = 33;int i1 = temp_S_1.compareTo(temp_S_2);System.out.println("temp_S_1 - temp_S_2 = " + i1);System.out.println("------------------------------------");//5 —— double doubleValue() : 返回當前Short對象的值,以double基本類型作接收double temp_d_0 = temp_S_0.doubleValue();System.out.println("double類型變量temp_d_0 = " + temp_d_0);System.out.println("------------------------------------");//6 —— int intValue() : 返回當前Short對象的值,以int基本類型作接收int temp_i_0 = temp_S_0.intValue();System.out.println("int類型變量temp_i_0 = " + temp_i_0);System.out.println("------------------------------------");//7 —— static int parseShort(String xxx) : 字符串類型 ——> short基本類型short temp_s_3 = Short.parseShort("128");System.out.println("short類型變量temp_s_3 = " + temp_s_3);System.out.println("------------------------------------");//8 —— String toString() : 將當前Short對象的值轉換為String類型Short temp_S_3 = 1277;String string_0 = temp_S_3.toString();System.out.println("Short類型對象temp_S_3的字符串形式為:" + string_0);System.out.println("------------------------------------");//9 —— static String toString(short s) : 將指定的short值轉換為String對象short temp_s_4 = 2;String string_1 = Short.toString(temp_s_4);System.out.println("short類型變量temp_s_4的字符串形式為:" + string_1);System.out.println("----------------------------------");//10 —— static Short valueOf(...) : 字符串類型 ——> Short類型Short temp_S_4 = Short.valueOf("1111");System.out.println("Short類型對象temp_S_4的值 = " + temp_S_4);}
}

5.3 Integer類常用方法

① Integer.MIN_VALUE、Integer.MAX_VALUE:返回 Integer/int 類型的最小值、最大值

② int?intValue() 、double doubleValue():返回當前 Integer 類對象對應的值,以 int/double 類型作接收

③?static int compare(int x, int?y):?比較兩個 int 變量的值。返回值:如果前一個數大,返回 1 ;如果前一個數小,返回 -1 ;相等則返回 0

④?int compareTo(Integer anotherInteger):比較兩個 Integer 類對象的值,返回值同

⑤ static int parseInt(String xxx):字符串類型 ——>? int 類型

⑥ String toString():?將當前 Integer 對象的值轉換為 String 類型

⑦ static String toString(short s):將指定的 int 值轉換為 String 對象

⑧ static Short valueOf(...):字符串類型 ——> Integer 類型

?static int max(int x, int y) 和 min(int x, int y):獲取兩個數中的最大值和最小值

static int sum(int x, int y):返回(x + y)的值

案例演示

public class demo {public static void main(String[] args) {//演示Integer類常用方法//1 —— Integer.MIN_VALUE,Integer.MAX_VALUE,返回Integer/int類型的最小值、最大值System.out.println(Integer.MIN_VALUE);System.out.println(Integer.MAX_VALUE);System.out.println("------------------------------------");//2 —— int intValue()、double doubleValue() : 返回當前Integer對象的值,以int/double基本類型作接收Integer temp_I_0 = 1280;       //自動裝箱int temp_i_0 = temp_I_0.intValue();double temp_d_0 = temp_I_0.doubleValue();System.out.println("int類型變量temp_i_0 = " + temp_i_0);System.out.println("double類型變量temp_d_0 = " + temp_d_0);System.out.println("------------------------------------");//3 —— static int compare(int x, int y) : 比較兩個int變量的值。如果前一個數大,返回1;如果前一個數小,返回-1;相等則返回0int temp_i_1 = 7;int temp_i_2 = 11;int i = Integer.compare(temp_i_1, temp_i_2);System.out.println("temp_i_1和temp_i_2,如果前一個數大,返回1;如果前一個數小,返回-1;相等則返回0 : " + i);System.out.println("------------------------------------");//4 —— int compareTo(Integer anotherInteger) : 比較兩個Integer類對象的值,返回值同方法3Integer temp_I_1 = 77;Integer temp_I_2 = 11;int i1 = temp_I_1.compareTo(temp_I_2);System.out.println("temp_I_1和temp_I_2,如果前一個數大,返回1;如果前一個數小,返回-1;相等則返回0 : " + i1);System.out.println("------------------------------------");//5 —— static int parseInt(String xxx) : 字符串類型 ——> int基本類型int temp_i_3 = Integer.parseInt("4444");System.out.println("int類型變量temp_i_3 = " + temp_i_3);System.out.println("------------------------------------");//6 —— String toString() : 將當前Integer對象的值轉換為String類型Integer temp_I_3 = 11217;String string_0 = temp_I_3.toString();System.out.println("Integer類型對象temp_I_3的字符串形式為:" + string_0);System.out.println("------------------------------------");//7 —— static String toString(int s) : 將指定的int值轉換為String對象int temp_i_4 = 111111;String string_1 = Integer.toString(temp_i_4);System.out.println("int類型變量temp_i_4的字符串形式為:" + string_1);System.out.println("----------------------------------");//8 —— static Integer valueOf(...) : 字符串類型 ——> Integer類型Integer temp_I_4 = Integer.valueOf("1111");System.out.println("Integer類型對象temp_I_4的值 = " + temp_I_4);System.out.println("----------------------------------");//9 —— static int max(int x, int y) 和 min(int x, int y) : 獲取兩個數中的最大值和最小值System.out.println("100和101哪個數更大?" + Integer.max(100, 101));System.out.println("200和201哪個數更小?" + Integer.min(200, 201));System.out.println("----------------------------------");//10 —— static int sum(int x, int y) : 返回(x + y)的值System.out.println("100 + 201 = " + Integer.sum(100 ,201));}
}

5.4 Long類常用方法

① Long.MIN_VALUE、Long.MAX_VALUE:返回 Long/long 類型的最小值、最大值

② long longValue()、int?intValue() 、double doubleValue():返回當前 Long 類對象對應的值,以 long/int/double 類型作接收

③?static int compare(long x, long?y):比較兩個 long 變量的值。返回值:如果前一個數大,返回 1 ;如果前一個數小,返回 -1 ;相等則返回 0

④?int compareTo(Long anotherLong):比較兩個 Long 類對象的值,返回值同?

⑤ static long parseLong(String xxx):字符串類型 ——> long 類型

⑥ String toString():將當前 Long 對象的值轉換為 String 類型

⑦ static String toString(long l):將指定的 long 值轉換為 String 對象

⑧ static Long valueOf(...):字符串類型 ——> Long 類型

?static long?max(long?x, long?y) 和 min(long?x, long?y):獲取兩個數中的最大值和最小值

static long?sum(long?x, long?y):返回(x + y)的值

案例演示

public class demo {public static void main(String[] args) {//演示Long類常用方法//1 —— Long.MIN_VALUE,Long.MAX_VALUE,Long/long類型的最小值、最大值System.out.println(Long.MIN_VALUE);System.out.println(Long.MAX_VALUE);System.out.println("------------------------------------");//2 —— long longValue()、int intValue()、double doubleValue(): 返回當前Long對象的值,以long/int/double基本類型作接收Long temp_L_0 = 2224L;       //自動裝箱long temp_l_0 = temp_L_0.longValue();int temp_i_0 = temp_L_0.intValue();double temp_d_0 = temp_L_0.doubleValue();System.out.println("long類型變量temp_l_0 = " + temp_l_0);System.out.println("int類型變量temp_i_0 = " + temp_i_0);System.out.println("double類型變量temp_d_0 = " + temp_d_0);System.out.println("------------------------------------");//3 —— static int compare(long x, long y) : 比較兩個long變量的值. 如果前一個數大,返回1;如果前一個數小,返回-1;相等則返回0long temp_l_1 = 222L;long temp_l_2 = 111L;int i = Long.compare(temp_l_1, temp_l_2);System.out.println("temp_l_1和temp_l_2,如果前一個數大,返回1;如果前一個數小,返回-1;相等則返回0 : " + i);System.out.println("------------------------------------");//4 —— int compareTo(Long anotherLong) : 比較兩個Long類對象的值,返回值同方法3Long temp_L_1 = 773L;Long temp_L_2 = 113L;int i1 = temp_L_1.compareTo(temp_L_2);System.out.println("temp_L_1和temp_L_2,如果前一個數大,返回1;如果前一個數小,返回-1;相等則返回0 : " + i1);System.out.println("------------------------------------");//5 —— static long parseLong(String xxx) : 字符串類型 ——> long基本類型long temp_l_3 = Long.parseLong("35252");System.out.println("long類型變量temp_l_3 = " + temp_l_3);System.out.println("------------------------------------");//6 —— String toString() : 將當前Long對象的值轉換為String類型Long temp_L_3 = 11217L;String string_0 = temp_L_3.toString();System.out.println("Long類型對象temp_L_3的字符串形式為:" + string_0);System.out.println("------------------------------------");//7 —— static String toString(long l) : 將指定的long值轉換為String對象long temp_l_4 = 222222;String string_1 = Long.toString(temp_l_4);System.out.println("long類型變量temp_l_4的字符串形式為:" + string_1);System.out.println("----------------------------------");//8 —— static Long valueOf(...) : 字符串類型 ——> Long類型Long temp_L_4 = Long.valueOf("111241");System.out.println("Long類型對象temp_L_4的值 = " + temp_L_4);System.out.println("----------------------------------");//9 —— static long max(long x, long y) 和 min(long x, long y) : 獲取兩個數中的最大值和最小值System.out.println("10000和10100哪個數更大?" + Long.max(10000, 10100));System.out.println("20000和20100哪個數更小?" + Long.min(20000, 20100));System.out.println("----------------------------------");//10 —— static long sum(long x, long y) : 返回(x + y)的值System.out.println("11111111 + 8888889 = " + Long.sum(11111111 ,8888889));}
}

5.5 Character類常用方法?

① char charValue():返回當前 Character 類對象對應的值,以 char 類型作接收

② static Character?valueOf(...):字符串類型 ——> Character?類型

③?static int compare(char x, char?y):比較兩個 Character 類對象的字符。返回值: xASCII 碼值 - yASCII 碼值

④?int compareTo(Character anotherCharater):比較兩個 Character 類對象的值,返回值同?

⑤??

(1)static boolean isDigit(char c1) : 判斷該字符是不是數字
(2)static boolean isLetter(char c2) : 判斷該字符是不是字母
(3)static boolean isUpperCase(char c3) : 判斷該字符是不是大寫形式
(4)static boolean isLowerCase(char c4) : 判斷該字符是不是小寫形式
(5)static boolean isWhitespace(char c5) : 判斷該字符是不是空格

⑥?

(1)static char toUpperCase(char c) : 將該字符轉換為大寫形式,以char類型作接收
(2)static char toLowerCase(char c) : 將該字符轉換為小寫形式,以char類型作接收

案例演示?

public class demo {public static void main(String[] args) {//演示 : Character類常用方法//1 —— char charValue():返回當前Character類對象對應的值,以char類型作接收Character character_0 = '蔡';//自動裝箱char char_0 = character_0.charValue();System.out.println("char基本類型變量char_0 = " + char_0);System.out.println("----------------------------------");//2 ——  static Character valueOf(...) :字符串類型 ——> Character類型Character character_1 = Character.valueOf('S');System.out.println("Character類對象character_1的字符是:" + character_0);System.out.println("----------------------------------");//3 —— static int compare(char x, char y) : 返回前面字符ASCII碼值 - 后面字符ASCII值的int類型int i1 = Character.compare('A', 'F');System.out.println("ASCII碼值'A' - 'F' = " + i1);System.out.println("----------------------------------");//4 —— int compareTo(Character anotherCharacter) : 比較兩個Character類對象的字符,返回值同方法2Character character_2 = 'a';Character character_3 = 'd';int i2 = character_2.compareTo(character_3);System.out.println("character_2 - character_3 = " + i2);System.out.println("----------------------------------");/*5—— static boolean isDigit(char c1) : 判斷該字符是不是數字—— static boolean isLetter(char c2) : 判斷該字符是不是字母—— static boolean isUpperCase(char c3) : 判斷該字符是不是大寫形式—— static boolean isLowerCase(char c4) : 判斷該字符是不是小寫形式—— static boolean isWhitespace(char c5) : 判斷該字符是不是空格*/System.out.println("\'A\'是不是數字 : " + Character.isDigit('A'));System.out.println("\'A\'是不是字母 : " + Character.isLetter('A'));System.out.println("\'A\'是不是大寫形式 : " + Character.isUpperCase('A'));System.out.println("\'A\'是不是小寫形式 : " + Character.isLowerCase('A'));System.out.println("\'A\'是不是空格 : " + Character.isWhitespace('A'));System.out.println("----------------------------------");/*6—— static char toUpperCase(char c) : 將該字符轉換為大寫形式,以char類型作接收—— static char toLowerCase(char c) : 將該字符轉換為小寫形式,以char類型作接收*/char c1 = Character.toUpperCase('n');char c2 = Character.toLowerCase('B');System.out.println("\'n\'字符的大寫形式為:" + c1);System.out.println("\'B\'字符的小寫形式為:" + c2);}
}

5.6 Float類常用方法

① Float.MIN_VALUE、Float.MAX_VALUE:返回 Float/float 類型的最小值、最大值

② float?floatValue()、int?intValue() 、long doubleValue()、double doubleValue():返回當前 Float 類對象對應的值,以 float/int/long/double 類型作接收

③?static int compare(float?x, float?y):比較兩個 float?變量的值。返回值:如果前一個數大,返回 1 ;如果前一個數小,返回 -1 ;相等則返回 0

④?int compareTo(Float anotherFloat):比較兩個 Float 類對象的值,返回值同

⑤ static float parseFloat(String xxx):字符串類型 ——> float 類型

⑥ String toString():將當前 Float 對象的值轉換為 String 類型

⑦ static String toString(float f):將指定的 float 值轉換為 String 對象

⑧ static Float valueOf(...):字符串類型 ——> Float 類型

⑨?static float max(float x, float y) 和 min(float x, float y):獲取兩個數中的最大值和最小值

⑩ static float sum(float x, float y):返回(x + y)的值

案例演示?

public class demo {public static void main(String[] args) {//演示 : Float類常用方法//1 —— Float.MIN_VALUE,Float.MAX_VALUE,Float/float類型的最小值、最大值System.out.println(Float.MIN_VALUE);System.out.println(Float.MAX_VALUE);System.out.println("------------------------------------");//2 —— float floatValue()、int intValue()、long longValue()、double doubleValue: 返回當前Float對象的值,以float/int/long/double基本類型作接收。Float temp_F_0 = 1024.11F;//自動裝箱float temp_f_0 = temp_F_0.floatValue();int temp_i_0 = temp_F_0.intValue();long temp_l_0 = temp_F_0.longValue();double temp_d_0 = temp_F_0.doubleValue();System.out.println("float類型變量temp_f_0 = " + temp_f_0);System.out.println("int類型變量temp_i_0 = " + temp_i_0);System.out.println("long類型變量temp_l_0 = " + temp_l_0);System.out.println("double類型變量temp_d_0 = " + temp_d_0);System.out.println("------------------------------------");//3 —— static int compare(float x, float y) : 比較兩個float變量的值, 如果前一個數大,返回1;如果前一個數小,返回-1;相等則返回0float temp_f_1 = 222.11F;float temp_f_2 = 222.11F;int i = Float.compare(temp_f_1, temp_f_2);System.out.println("temp_f_1和temp_f_2,如果前一個數大,返回1;如果前一個數小,返回-1;相等則返回0 : " + i);System.out.println("------------------------------------");//4 —— int compareTo(Float anotherFloat) : 比較兩個Float類對象的值,返回值同方法3Float temp_F_1 = 222.11F;Float temp_F_2 = 123.11F;int i1 = temp_F_1.compareTo(temp_F_2);System.out.println("temp_F_1和temp_F_2,如果前一個數大,返回1;如果前一個數小,返回-1;相等則返回0 : " + i1);System.out.println("------------------------------------");//5 —— static float parseFloat(String xxx) : 字符串類型 ——> float基本類型float temp_f_3 = Float.parseFloat("35252.11125");System.out.println("float類型變量temp_f_3 = " + temp_f_3);System.out.println("------------------------------------");//6 —— String toString() : 將當前Float對象的值轉換為String類型Float temp_F_3 = 12144217.12F;String string_0 = temp_F_3.toString();System.out.println("Float類型對象temp_F_3的字符串形式為:" + string_0);System.out.println("------------------------------------");//7 —— static String toString(float f) : 將指定的float值轉換為String對象float temp_f_4 = 222222.11F;String string_1 = Float.toString(temp_f_4);System.out.println("float類型變量temp_f_4的字符串形式為:" + string_1);System.out.println("----------------------------------");//8 —— static Float valueOf(...) : 字符串類型 ——> Float類型Float temp_F_4 = Float.valueOf("111241.1235");System.out.println("Float類型對象temp_F_4的值 = " + temp_F_4);System.out.println("----------------------------------");//9 —— static float max(float x, float y) 和 min(float x, float y) : 獲取兩個數中的最大值和最小值System.out.println("10000.00 和 10100.11, 哪個數更大?" + Float.max(10000.00F, 10100.11F));System.out.println("200.00 和 201.88, 哪個數更小?" + Float.min(200.00F, 201.88F));System.out.println("----------------------------------");//10 —— static float sum(float x, float y) : 返回(x + y)的值System.out.println("11111.11 + 8889.022 = " + Float.sum(11111.11F,8889.022F));}
}

5.7 Double類常用方法

Float 類完全一致,這里不再贅述

5.8 Boolean類常用方法

① boolean booleanValue():返回當前 Boolean 類對象對應的值,以 boolean 類型作接收

② static int compare(boolean x,boolean y):比較兩個 boolean 變量的值,兩個變量真值相同返回 0 .否則返回值取決于于第一個 boolean 變量的真值,true 返回 1false 返回 -1

③?int compareTo(Boolean anotherBoolean):比較兩個 Boolean 類對象的值,返回值同

④ static boolean parseBoolean(String xxx):字符串類型 ——>? boolean 類型

⑤?String toString():將當前 Boolean 對象的值轉換為 String 類型

⑥?static String toString(boolean b):將指定的 boolean 值轉換為 String 對象

⑦ static Boolean valueOf(...) :字符串類型 ——> Boolean 類型

?案例演示

public class demo {public static void main(String[] args) {//演示 : Boolean類常用方法//1 —— boolean booleanValue():返回當前Boolean類對象對應的值,以boolean類型作接收Boolean temp_B_0 = true;//自動裝箱boolean temp_b_0 = temp_B_0.booleanValue();System.out.println("boolean類型變量temp_b_0 = " + temp_b_0);System.out.println("------------------------------------");//2 —— static int compare(boolean x, boolean y) : 比較兩個boolean變量的值,兩個變量真值相同返回0。否則返回值取決于第一個boolean變量的真值,true返回1,false返回-1.boolean temp_b_1 = false;boolean temp_b_2 = true;int i = Boolean.compare(temp_b_1, temp_b_2);int ii = Boolean.compare(temp_b_2, temp_b_1);int iii = Boolean.compare(temp_b_2, temp_b_2);System.out.println("temp_b_1和temp_b_2, 兩個真值相同返回1。否則返回值取決于傳入第一個boolean變量的真值,true返回1,false返回-1 : " + i);System.out.println("temp_b_2和temp_b_1, 兩個真值相同返回1。否則返回值取決于傳入第一個boolean變量的真值,true返回1,false返回-1 : " + ii);System.out.println("temp_b_2和temp_b_2, 兩個真值相同返回1。否則返回值取決于傳入第一個boolean變量的真值,true返回1,false返回-1 : " + iii);System.out.println("------------------------------------");//3 —— int compareTo(Boolean anotherBoolean) : 比較兩個Boolean類對象的值,返回值同方法2Boolean temp_B_1 = false;Boolean temp_B_2 = false;int i1 = temp_B_1.compareTo(temp_B_2);System.out.println("temp_B_1和temp_B_2的真值情況是 : " + i1);System.out.println("------------------------------------");//4 —— static boolean parseBoolean(String xxx) : 字符串類型 ——> boolean基本類型boolean temp_b_3 = Boolean.parseBoolean("666");System.out.println("boolean類型變量temp_b_3 = " + temp_b_3);System.out.println("------------------------------------");//5 —— String toString() : 將當前Boolean對象的值轉換為String類型Boolean temp_B_3 = false;String string_0 = temp_B_3.toString();System.out.println("Boolean類型對象temp_B_3的字符串形式為:" + string_0);System.out.println("------------------------------------");//6 —— static String toString(boolean s) : 將指定的boolean值轉換為String對象boolean temp_b_4 = true;String string_1 = Boolean.toString(temp_b_4);System.out.println("boolean類型變量temp_b_4的字符串形式為:" + string_1);System.out.println("----------------------------------");//7 —— static Boolean valueOf(...) : 字符串類型 ——> Boolean類型Boolean temp_B_4 = Boolean.valueOf("false");System.out.println("Boolean類型對象temp_B_4的值 = " + temp_B_4);}
}

6.Integer創建機制的面試題(重要)

?Integer.valueOf 的源碼在本文的 3.3自動拆裝箱 已經提到,這里不作贅述

② 基本數據類型與其對應的包裝類用 "==" 比較時,比較的是值是否相等

6.1 練習題1

判斷輸出結果

public class demo{public static void main(String[] args){Integer i = new Integer(1);Integer j = new Integer(1);System.out.println(i==j);//false,i與j是兩個不同的對象Integer m = 1;//自動裝箱,底層調用的是Integer.valueOf(1),從緩沖數組cache中取IntegerInteger n = 1;//自動裝箱,底層調用的是Integer.valueOf(1),從緩沖數組cache中取IntegerSystem.out.println(m==n);//trueInteger x = 128;//自動裝箱,底層調用的是Integer.valueOf(128),new一個新的IntegerInteger y = 128;//自動裝箱,底層調用的是Integer.valueOf(128),new一個新的IntegerSystem.out.println(x==y);//false}
}

6.2 練習題2

判斷輸出結果

public class demo{public static void main(String[] args){Integer i1 = 127;//自動裝箱,底層調用的是Integer.valueOf(127),從緩沖數組cache中取IntegerInteger i2 = new Integer(127);//new一個新的IntegerSystem.out.println(i1==i2);//falseInteger i3 = 127;//自動裝箱,底層調用的是Integer.valueOf(127),從緩沖數組cache中取Integerint i4 = 127;System.out.println(i3==i4);//true,判斷的是值是否相等Integer i5 = 128;//自動裝箱,底層調用的是Integer.valueOf(128),new一個新的Integerint i6 = 128;System.out.println(i5==i6);//true,判斷的是值是否相等}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/pingmian/73790.shtml
繁體地址,請注明出處:http://hk.pswp.cn/pingmian/73790.shtml
英文地址,請注明出處:http://en.pswp.cn/pingmian/73790.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【Qt】QByteArray詳解

QByteArray 是 Qt 框架中用于處理原始字節數據的核心類&#xff0c;其實質可以概括為以下幾點&#xff1a; 1. 底層數據結構 ? 連續內存塊&#xff1a;存儲一段連續的字節數據&#xff08;char*&#xff09;&#xff0c;類似 std::vector<char>&#xff0c;但針對 Qt 框…

Stable Diffusion vue本地api接口對接,模型切換, ai功能集成開源項目 ollama-chat-ui-vue

1.開啟Stable Diffusion的api服務 編輯webui-user.bat 添加 –api 開啟api服務&#xff0c;然后保存啟動就可以了 2.api 文檔地址 http://127.0.0.1:7860/docs3. 文生圖 接口 地址 /sdapi/v1/txt2img //post 請求入參 {enable_hr: false, // 開啟高清hrdenoising_stre…

CentOS 7 部署RuoYi 項目

換源 備份現有的 YUM 源配置文件 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup 默認的 CentOS 官方鏡像源替換為阿里云的鏡像源&#xff0c;以提高下載速度和穩定性。 curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.co…

從 WPF 到 MAUI:跨平臺 UI 開發的進化之路

一、引言 在軟件開發領域&#xff0c;用戶界面&#xff08;UI&#xff09;開發一直是至關重要的環節。隨著技術的不斷發展&#xff0c;開發者對于創建跨平臺、高性能且美觀的 UI 需求日益增長。Windows Presentation Foundation&#xff08;WPF&#xff09;和 .NET Multi - pl…

C++ stack容器總結

stack 基本概念 概念&#xff1a; stack是一種后進先出(Last In First Out, LIFO)的數據結構&#xff0c;它只有一個出口 棧中只有頂端的元素才可以被外界使用&#xff0c;因此棧不允許有遍歷行為 棧中進入的數據稱為----入棧&#xff08;PUSH&#xff09; 棧中出去的數據成…

【SDMs分析1】基于ENMTools R包的生態位分化分析和圖像繪制(identity.test())

基于ENMTools包的生態位分化 1. 寫在前面2. 生態位分化檢驗案例13. 生態位分化檢驗案例21. 寫在前面 最近學了一個新的內容,主要是關于兩個物種之間生態位分化檢驗的 R 語言代碼。生態位分化是物種分布模型(SDM )研究中的關鍵部分,許多 SCI 論文都會涉及這一分析。該方法主…

SpringBoot 7 種實現 HTTP 調用的方式

1. HttpClient HttpClient是Apache基金會提供的一個用于發送HTTP請求的Java客戶端庫。 盡管它功能強大&#xff0c;但由于其API設計較為復雜且包體積龐大&#xff0c;在一些輕量級的應用場景中可能顯得過于臃腫。 不過&#xff0c;在需要高度定制化的HTTP請求時&#xff0c;H…

Ubuntu與Windows之間相互復制粘貼的方法

一、打開Ubuntu終端 二、卸載已有的工具 sudo apt-get autoremove open-vm-tools 三、安裝工具 sudo apt-get install open-vm-tools-desktop 四、重啟 直接輸入reboot 注&#xff1a;有任何問題歡迎評論區交流討論或者私信&#xff01;

ECharts實現數據可視化

ECharts實現數據可視化 一、Echarts的簡介二、Echarts使用教程1.下載echarts.min.js文件2.編寫echarts代碼&#xff08;1&#xff09;創建渲染實列&#xff08;2&#xff09;修改option達到預期的效果&#xff08;3&#xff09;創建配置項到實例中 三、Echarts的基礎配置四、前…

ArcGIS 10.8.1之后發布柵格數據的MapServer 動態工作空間 替換數據源渲染問題

背景 經過測試&#xff0c;Server 10.8.1、11.0、11.1發布相關服務設置動態空間之后&#xff0c;前端都無法自動讀取同名的clr色彩映射表文件進行渲染&#xff0c;服務都是由ArcGIS Pro進行發布。 原因 基于ArcMap發布的服務才支持&#xff0c;但是10.8.1之后不支持ArcMap發…

vscode在使用 alt + tab 切換程序窗口時,輸入法總是自動變為中文模式

因為需要在 vscode 中編寫代碼&#xff0c;將輸入法設為英文模式&#xff0c;但是用 alt tab 切換到瀏覽器查看文檔&#xff0c;此時瀏覽器也是英文模式&#xff0c;但是再切回 vscode 后就變為中文模式了&#xff0c;需要使用 shift 鍵切換為英文模式&#xff0c;一次兩次還好…

【Linux加餐-網絡命令】

一、Ping命令 Ping 是一種網絡工具&#xff0c;用于測試主機之間的連通性。它通過發送 ICMP&#xff08;Internet Control Message Protocol&#xff09;回顯請求 報文到目標主機&#xff0c;并等待目標主機返回 ICMP 回顯應答 報文&#xff0c;從而判斷網絡是否通暢以及測量往…

Maven工具學習使用(六)——聚合與繼承

Maven的聚合特性能夠把項目的各個模塊聚合在一起構建,而Maven的繼承特性則能幫助抽取個模塊相同的依賴和插件等配置,在簡化POM的同時,還能促進各個模塊配置的一致性。 一般說來一個項目的子模塊都應該使用同樣的groupId,如果他們一起開發和發布,還應該使用同樣的version,…

vulhub靶場jangow-01-1.0.1

啟動靶機時點shift停在這個界面 點e進入編輯頁面&#xff0c;把ro改成rw signie init/bin/bash Ctrlx保存&#xff0c;ip a查看網卡信息 vim /etc/network/interfaces 把enp0s17改為ens33&#xff0c;保存退出 重啟靶機&#xff0c;nmap掃ip ip為192.168.93.179 nmap掃端口 掃…

C++11QT復習 (四)

Day6-1 輸入輸出流運算符重載&#xff08;2025.03.25&#xff09; 1. 拷貝構造函數的調用時機 2. 友元2.1 友元函數 3. 輸入輸出流運算符重載3.1 關鍵知識點3.2 代碼3.3 關鍵問題3.4 完整代碼 4. 下標訪問運算符 operator[]4.1 關鍵知識點4.2 代碼 5. 函數調用運算符 operator…

數倉架構告別「補丁」時代!全新批流一體 Domino 架構終結“批流縫合”

在數字化轉型的浪潮中&#xff0c;企業對數據處理的需求日益復雜多變&#xff0c;傳統的批處理和流處理架構已難以滿足日益增長的性能和時效性要求。在此背景下&#xff0c;YMatrix CEO 姚延棟發布了深度文章《數倉架構告別「補丁」時代&#xff01;全新批流一體 Domino 架構終…

一文詳解QT環境搭建:ubuntu20.4安裝配置Qt5

隨著軟件開發技術的不斷進步&#xff0c;跨平臺應用程序的需求日益增長&#xff0c;開發者們面臨著如何在不同操作系統之間保持代碼的一致性和效率的問題。Qt作為一個成熟的跨平臺C框架&#xff0c;在這方面提供了卓越的支持&#xff0c;不僅簡化了GUI應用程序的創建過程&#…

安全+低碳+高效:Acrel-3000助力企業打造未來型電能管理體系-安科瑞黃安南

一 背景 電能因為方便傳輸、易于轉換、便于控制等特性&#xff0c;成為廣大企事業單位生產、辦公最主要的能量來源。雙碳背景下&#xff0c;由于電能清潔、高效、零排放的特點&#xff0c;能源消費側將逐步以電代煤、以電代油、以電代氣&#xff0c;形成以電為中心的能源消費體…

Docker 安裝 RabbitMQ

以下是在Docker中安裝RabbitMQ并實現配置、數據、日志文件映射的完整步驟。 步驟 1&#xff1a;創建本地目錄結構 # 創建配置、數據、日志目錄 mkdir -p /root/docker/rabbitmq/{conf,data,logs}# 目錄結構說明&#xff1a; # - conf: 存放自定義配置文件 # - data: 持久化存儲…

SAP-ABAP:SAP數據集成全場景技術指南(BAPI、RFC、IDOC、BATCHJOB、ODATA、WEBSERVICE):從實時交互到批量處理

SAP數據集成全場景技術指南:從實時交互到批量處理 #mermaid-svg-hpPMerJYUerla0BJ {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-hpPMerJYUerla0BJ .error-icon{fill:#552222;}#mermaid-svg-hpPMerJYUerla0BJ .er…