java 包装类 万字详解(通俗易懂)
前言
简介和溯源
拆装箱
String类和基本类型的相互转化
String类和包装类型的相互转化
八大包装类的常用方法汇总(含代码演示)
一、前言 :
本节内容是我们《API-常用类》专题的最后一节了。本节内容主要讲包装类,内容包括但不限于包装类的诞生,包装类的类图,拆装箱,String类和基本类型/包装类型间的相互转化,以及八大包装类常用方法的演示。up希望能通过IDEA类图,Debug,代码演示等多种方式,帮助大家快速上手并理解java包装类。注意 : ①代码中的注释也很重要;②不要眼高手低,自己敲一遍才能知道怎么用。③点击侧边栏目录可以跳转。良工不示人以朴,所有文章都会适时改进。感谢阅读!
二、简介和溯源
1.简介 :
基本数据类型不是对象,不能使用类的方法;因此,java针对基本类型提供了它们对应的包装类,八大基本数据类型,对应了八种包装类,以对象的形式来调用。包装类有了类的特点,使我们可以调用包装类中的方法。
包装类属于java.base模块,java.lang包下,如下图所示 :

可以看到,在形式上除了Integer和Character这两个包装类外,其他六个包装类的类名均是对应的基本类型首字母大写后得到的。
当然,形式上的东西再咋滴也没有那么重要,那么这些个包装类在类与类的关系上有什么不同呢?下面我们来看看这些个包装类分别是什么来头。
2.溯源 :
IDEA最常用的17个快捷键中,Ctrl + h/H 快捷键可以快速当前类的继承关系,并且在IDEA中,还可以继而查看当前类继承关系的图表表示。
先来看一下Integer类的继承关系图表 :

可以看到,Integer类继承了Number类,并且实现了若干接口。
现在,利用Ctrl + Alt + b/B 的快捷键,将其他七大包装类分别添加进入该图标,如下GIF所示 :

最后的效果图如下所示 :

哈哈😂,是不是感觉自己被耍了,这**是什么玩意儿?乱七八糟的! 欸,别急,仔细观察这张图,你只需要记住一个结论:除了Boolean和Character这两个包装类外,其他六大包装类都继承自Number类,并且它们都实现了一些接口。
那么,Boolean类和Character类的类关系图又是个什么情况呢?
Boolean类的类关系图如下 :

Character类的关系图如下 :

可以看到,Boolean类和Character类都没有继承Number类,它们都实现了一些接口,并且实现的接口还不尽相同。当然,你只需要记住Boolean类和Character类与其他六大包装类的区别就行,你也不需要去背它们的关系图,因为背了也没个🐔儿用。
三、装箱和拆箱
1.介绍
装箱 : 基本类型 ——> 包装类型(或者叫对象类型,引用类型)
拆箱 : 包装类型 ——> 基本类型
2.手动拆装箱 :
JDK5.0之前,拆装箱均是手动完成的。
手动装箱,可以使用包装类的构造器来完成,也可以使用valueOf() 方法。
手动拆箱,以Integer类为例,需要用到intValue() 方法。
演示 :
以Integer包装类为例,以Intro类为演示类。
代码如下 :
package csdn.knowledge.api.Integer;public class Intro {public static void main(String[] args) {//JDK5.0之前,拆装箱都是手动完成。int temp = 19;//手动装箱(基本类型 ————> 包装/引用类型)Integer integer_0 = new Integer(temp); //划线表示该构造方法已过时Integer integer_1 = new Integer(11);Integer integer_2 = Integer.valueOf(temp);//手动拆箱(包装/引用类型 ————> 基本类型)int tempI_0 = integer_0.intValue(); /**该方法的接收类型为int类型*/System.out.println("integer_0的值 = " + integer_0);System.out.println("integer_1的值 = " + integer_1);System.out.println("integer_2的值 = " + integer_2);System.out.println("tempI_0 = " + tempI_0);System.out.println("----------------------------------");}
}
运行结果 :

3.自动拆装箱 :
JDK5.0开始,java提供了自动拆装箱的机制。(不需要手动调用构造器或者方法了)
自动拆箱 : 实际上底层仍然调用了valueOf() 方法。
自动装箱 : 实际上底层仍然调用了intValue() 方法(以Integer包装类为例)
演示 :
以Integer包装类为例,以Intro类为演示类。
代码如下 :
package csdn.knowledge.api.Integer;public class Intro {public static void main(String[] args) {//JDK5.0以后,java提供了自动拆装箱Integer integer_3 = 199; //(自动)装箱——其实底层调用的仍然是valueOf方法(可Debug)int tempI_1 = integer_3; //(自动)拆箱——其实底层调用的仍然是intValue方法System.out.println("integer_3的值 = " + integer_3);System.out.println("tempI_1 = " + integer_3);System.out.println("----------------------------------");}
}
运行结果 :

我们可以通过Debug来验证自动拆装箱实际在底层调用了手动拆装箱时用到的方法。如下GIF图 :

可以看到,明明是自动装箱,可我们选择强制跳入方法时,依然跳入了valueOf() 方法;又明明是自动拆箱,可我们选择强制跳入方法时,依然跳入了intValue() 方法。
这里还要再强调一点,关于valueOf() 方法,Integer类中的valueOf() 方法源码如下 :
@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对象🌶,而是调用底层的缓冲数组。通过追溯源码,我们可以得知这里的low和high 的实际范围是-128 ~ 127,如下图所示 :


并且,我们依然可以再次通过Debug来看看底层的缓冲数组是否真实存在,如下GIF所示 :


四、关于String类型的转化问题 :
1.String类型和基本类型的相互转化 :
①String类 ——> 基本类型
static 基本类型 parseXxx(String) :
包装类中的该方法可以将字符串类型的数据转换成对应的基本类型,需要用相应的基本类型来作接收。需要注意的是,在将字符串类型转为其他类型前,一定要确认字符串里面的内容能否正常转换,比方说,如果你想把”jdlsajflsajfl“这段字符串转换为int类型,那tm能行吗?这时候IDEA会报数字格式异常,如下图所示 :

演示 :
以Method类为演示类,代码如下 :
package csdn.knowledge.api.Integer;public class Method {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);}
}
运行结果 :

这时候,可能有眼尖的小伙伴儿发现了——这咋没有Character包装类捏?🤗
这是因为在八大包装类中,除了Character类外,其他的7种包装类中都有parseXxx() 方法。如果你想将字符串类型的数据转换成char类型的数据,你可以通过String类中的toCharArray() 方法和 charAt() 方法来做到。还记得我们在String类中讲到的这俩方法吗?
回顾一下 :
转换功能的方法 —— char[] toCharArray() : 将字符串转换成字符数组
获取功能的方法 —— char charAt(int index) : 获取指定索引位置的字符
以StringDemo类为演示类,代码如下 :
package csdn.knowledge.api.Integer;public class StringDemo {public static void main(String[] args) {//定义一个字符串String string = "CSDN_yyds";//利用toCharArray() 方法将字符串转换为字符数组char[] charArray = string.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 = string.charAt(0);char temp_char_1 = string.charAt(1);char temp_char_2 = string.charAt(2);char temp_char_3 = string.charAt(3);char temp_char_4 = string.charAt(4);char temp_char_5 = string.charAt(5);char temp_char_6 = string.charAt(6);char temp_char_7 = string.charAt(7);char temp_char_8 = string.charAt(8);System.out.println("string字符串第一个元素为:" + temp_char_0);System.out.println("string字符串第二个元素为:" + temp_char_1);System.out.println("string字符串第三个元素为:" + temp_char_2);System.out.println("string字符串第四个元素为:" + temp_char_3);System.out.println("string字符串第五个元素为:" + temp_char_4);System.out.println("string字符串第六个元素为:" + temp_char_5);System.out.println("string字符串第七个元素为:" + temp_char_6);System.out.println("string字符串第八个元素为:" + temp_char_7);System.out.println("string字符串第九个元素为:" + temp_char_8);}
}
运行结果:


②基本类型 ——> String类
基本类型要转字符串那就太简单了,最常见的两种方式——①直接与空字符串进行拼接,②String类的valueOf方法。
回顾一下 :
转换功能的方法 —— static String valueOf(...) : 将指定类型数据转换成字符串
演示 :
以StringDemo_1作为演示类,代码如下 :
package csdn.knowledge.api.Integer;public class StringDemo_1 {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);}
}
运行结果 :

2.String类型和包装类的相互转化 :
①String类 ——> 包装类
有两种方式,如下 :
方式一 : Integer integer_0 = Integer.parseInt(字符串类型变量);
方式二 : 利用包装类的构造器,例如 : Integer integer_1 = new Integer(字符串类型变量);
方式一中用到了parseInt方法,注意,我们在上文String类型转基本类型时就用到了parseInt方法,但我们当时是用int类型变量来作接收的。当然,parseInt方法的返回值类型本来就是int类型,如下图所示 :

因此,这里的方式一实际上应用了自动装箱,把等号右边返回的int类型的值,在底层又调用valueOf方法装箱成了Integer包装类。
演示 :
以StringDemo_为演示类,代码如下 :
package csdn.knowledge.api.Integer;public class StringDemo_ {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);}
}
运行结果 :

②包装类 ——> String类
有三种方式,如下 :
方式一 : String xxx = 包装类变量名 + "";
方式二 : String xxx = 包装类类名.toString();
方式三 : String xxx = String.valueOf(...);
方式一和我们上文中提到的基本类型转String类型的方式一回事儿;方式二体现出包装类相对于基本类型的优势,可以直接调用包装类中的方法;方式三也和我们上文中提到的基本类型转String类型的方式一回事儿。因此,这里就不重点演示了,仅以Integer类为例。
以StringDemo_2为演示类,代码如下 :
package csdn.knowledge.api.Integer;public class StringDemo_2 {public static void main(String[] args) {//演示 : 包装类 ————> String类//方式一 :Integer integer_0 = 141; //自动装箱String string_0 = integer_0 + "";//方式二 :Integer integer_1 = 135;String string_1 = Integer.toString(integer_1) + " hello";//方式三 :Integer integer_2 = 431;String string_2 = String.valueOf(integer_2) + " world";System.out.println("string_0 = " + string_0);System.out.println("string_1 = " + string_1);System.out.println("string_2 = " + string_2);}
}
运行结果 :

五、八大包装类的常用成员方法
0.前言 :
是否还记得我们在开篇中引入的包装类的类图,即下图 :

如果你觉得看这个已经够难受了,那你就错了。IDEA提供了功能,可以将类的方法在类图中直接显示出来。到时候你会头大的,如下GIF所示 :

可以看到,类图的转场效果还是比较nice的。但是,这么多方法,就算up真的要一一演示的话,不说我能不能撑住,我猜你是肯定撑不住😂。你不信?那好,1h内背完这张图里的所有方法,私信up领取1万块钱(bushi)。所以,up决定把每个包装类中比较典型的几个方法给提出来,再给大家演示演示,咱们都节省时间,你说对不对捏🤗。当然,开个玩笑哈。我们已经学习了API,那么之后如果有用到新的方法直接去API里查看就行,方便地很。
up会在代码中直接标出注释,注明该方法的具体功能以及该方法如何使用。所以,大家结合注释,直接看代码即可。
1.Byte类常用方法汇总 :
以Byte_类为演示类,代码如下 :
package csdn.knowledge.api.Integer.Eight;public class Byte_ {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() : 与方法1同理double bb1 = b.doubleValue();System.out.println("double类型变量bb1 = " + bb1);System.out.println("----------------------------------");//5 —— int intValue() : 与方法1同理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);}
}
运行结果 :

2.Short类常用方法汇总 :
以Short_类为演示类,代码如下 :
package csdn.knowledge.api.Integer.Eight;public class Short_ {public static void main(String[] args) {//演示Short类常用方法//1 —— 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("------------------------------------");//1_EX —— int intValue()//1_EX —— double doubleValue()//......等等同方法1格式一样的方法。用法原理与方法1相同。//2 —— 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("------------------------------------");//3 —— int compareTo(Short anotherShort) : 比较两个Short类对象的值,返回值同方法2Short 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("------------------------------------");//4 —— 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("------------------------------------");//5 —— 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("------------------------------------");//6 —— 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("----------------------------------");//7 —— static Short valueOf(...) : 字符串类型 ——> Short类型Short temp_S_4 = Short.valueOf("1111");System.out.println("Short类型对象temp_S_4的值 = " + temp_S_4);}
}
运行结果 :

3.Integer类常用方法汇总 :
以Integer_类为演示类,代码如下 :
package csdn.knowledge.api.Integer.Eight;public class Integer_ {public static void main(String[] args) {//演示Integer类常用方法//1 —— int intValue() : 返回当前Integer对象的值,以int基本类型作接收。Integer temp_I_0 = 1280; //自动装箱int temp_i_0 = temp_I_0.intValue();System.out.println("int类型变量temp_i_0 = " + temp_i_0);System.out.println("------------------------------------");/*1_EX —— int intValue()1_EX —— double doubleValue()......等等同方法1格式一样的方法。用法原理与方法1相同。*///2 —— static int compare(int x, int y) : 比较两个int变量的值。如果前一个数大,返回1;如果前一个数小,返回-1;相等则返回0。int 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("------------------------------------");//3 —— int compareTo(Integer anotherInteger) : 比较两个Integer类对象的值,返回值同方法2Integer 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("------------------------------------");//4 —— 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("------------------------------------");//5 —— 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("------------------------------------");//6 —— 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("----------------------------------");//7 —— static Integer valueOf(...) : 字符串类型 ——> Integer类型Integer temp_I_4 = Integer.valueOf("1111");System.out.println("Integer类型对象temp_I_4的值 = " + temp_I_4);System.out.println("----------------------------------");//8 —— 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("----------------------------------");//9 —— static int sum(int x, int y) : 返回(x + y)的值System.out.println("100 + 201 = " + Integer.sum(100 ,201));}
}
运行结果:

4.Long类常用方法汇总 :
以Long_类为演示类,代码如下 :
package csdn.knowledge.api.Integer.Eight;public class Long_ {public static void main(String[] args) {//演示Long类常用方法//1 —— long longValue() : 返回当前Long对象的值,以long基本类型作接收。Long temp_L_0 = 2224L; //自动装箱long temp_l_0 = temp_L_0.longValue();System.out.println("long类型变量temp_l_0 = " + temp_l_0);System.out.println("------------------------------------");/*1_EX —— int intValue()1_EX —— double doubleValue()......等等同方法1格式一样的方法。用法原理与方法1相同。*///2 —— static int compare(long x, long y) : 比较两个long变量的值. 如果前一个数大,返回1;如果前一个数小,返回-1;相等则返回0。long 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("------------------------------------");//3 —— int compareTo(Long anotherLong) : 比较两个Long类对象的值,返回值同方法2Long 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("------------------------------------");//4 —— 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("------------------------------------");//5 —— 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("------------------------------------");//6 —— 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("----------------------------------");//7 —— static Long valueOf(...) : 字符串类型 ——> Long类型Long temp_L_4 = Long.valueOf("111241");System.out.println("Long类型对象temp_L_4的值 = " + temp_L_4);System.out.println("----------------------------------");//8 —— 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("----------------------------------");//9 —— static long sum(long x, long y) : 返回(x + y)的值System.out.println("11111111 + 8888889 = " + Long.sum(11111111 ,8888889));}
}
运行结果 :

5.Character类常用方法汇总 :
以Character_类为演示类,代码如下 :
package csdn.knowledge.api.Integer.Eight;public class Character_ {public static void main(String[] args) {//演示 : Character类常用方法//1 —— 装箱拆箱 : valueOf() 和 charValue()Character character_0 = Character.valueOf('S');char char_0 = character_0.charValue();System.out.println("Character类对象character_0的字符是:" + character_0);System.out.println("char基本类型变量char_0 = " + char_0);System.out.println("----------------------------------");//2 —— 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("----------------------------------");//3 —— int compareTo(Character anotherCharacter) : 比较两个Character类对象的字符,返回值同方法2Character character_1 = 'a'; //自动装箱Character character_2 = 'd';int i2 = character_1.compareTo(character_2);System.out.println("character_1 - character_2 = " + i2);System.out.println("----------------------------------");//4 —— static boolean isDigit(char c1) : 判断该字符是不是数字//5 —— static boolean isLetter(char c2) : 判断该字符是不是字母//6 —— static boolean isUpperCase(char c3) : 判断该字符是不是大写形式//7 —— static boolean isLowerCase(char c4) : 判断该字符是不是小写形式//8 —— 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("----------------------------------");//9 —— static char toUpperCase(char c) : 将该字符转换为大写形式,以char类型作接收//10 —— 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);}
}
运行结果 :

6.Float类常用方法汇总 :
以Float_类为演示类,代码如下 :
package csdn.knowledge.api.Integer.Eight;public class Float_ {public static void main(String[] args) {//演示 : Float类常用方法//1 —— float floatValue() : 返回当前Float对象的值,以float基本类型作接收。Float temp_F_0 = 1024.11F; //自动装箱float temp_f_0 = temp_F_0.floatValue();System.out.println("float类型变量temp_f_0 = " + temp_f_0);System.out.println("------------------------------------");/*1_EX —— int intValue()1_EX —— double doubleValue()......等等同方法1格式一样的方法。用法原理与方法1相同。*///2 —— static int compare(float x, float y) : 比较两个float变量的值, 如果前一个数大,返回1;如果前一个数小,返回-1;相等则返回0。float 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("------------------------------------");//3 —— int compareTo(Float anotherFloat) : 比较两个Float类对象的值,返回值同方法2Float 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("------------------------------------");//4 —— 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("------------------------------------");//5 —— 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("------------------------------------");//6 —— 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("----------------------------------");//7 —— 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("----------------------------------");//8 —— 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("----------------------------------");//9 —— static float sum(float x, float y) : 返回(x + y)的值System.out.println("11111.11 + 8889.022 = " + Float.sum(11111.11F,8889.022F));}
}
运行结果 :

7.Double类常用方法汇总 :
以Double_类为演示类,代码如下 :
package csdn.knowledge.api.Integer.Eight;public class Double_ {public static void main(String[] args) {//演示 : Double类常用方法//1 —— double doubleValue() : 返回当前Double对象的值,以double基本类型作接收。Double temp_D_0 = 1024.5; //自动装箱double temp_d_0 = temp_D_0.doubleValue();System.out.println("double类型变量temp_d_0 = " + temp_d_0);System.out.println("------------------------------------");/*1_EX —— int intValue()1_EX —— double doubleValue()......等等同方法1格式一样的方法。用法原理与方法1相同。*///2 —— static int compare(double x, double y) : 比较两个double变量的值, 如果前一个数大,返回1;如果前一个数小,返回-1;相等则返回0。double temp_d_1 = 888.88;double temp_d_2 = 888.88;int i = Double.compare(temp_d_1, temp_d_2);System.out.println("temp_d_1和temp_d_2,如果前一个数大,返回1;如果前一个数小,返回-1;相等则返回0 : " + i);System.out.println("------------------------------------");//3 —— int compareTo(Double anotherDouble) : 比较两个Double类对象的值,返回值同方法2Double temp_D_1 = 123.1234;Double temp_D_2 = 1234.123;int i1 = temp_D_1.compareTo(temp_D_2);System.out.println("temp_D_1和temp_D_2,如果前一个数大,返回1;如果前一个数小,返回-1;相等则返回0 : " + i1);System.out.println("------------------------------------");//4 —— static double parseDouble(String xxx) : 字符串类型 ——> double基本类型double temp_d_3 = Double.parseDouble("35252.11125");System.out.println("double类型变量temp_d_3 = " + temp_d_3);System.out.println("------------------------------------");//5 —— String toString() : 将当前Float对象的值转换为String类型Double temp_D_3 = 3333144217.12;String string_0 = temp_D_3.toString();System.out.println("Double类型对象temp_D_3的字符串形式为:" + string_0);System.out.println("------------------------------------");//6 —— static String toString(double f) : 将指定的double值转换为String对象double temp_d_4 = 233.333333333;String string_1 = Double.toString(temp_d_4);System.out.println("double类型变量temp_d_4的字符串形式为:" + string_1);System.out.println("----------------------------------");//7 —— static double valueOf(...) : 字符串类型 ——> double类型Double temp_D_4 = Double.valueOf("66666.1235");System.out.println("Double类型对象temp_D_4的值 = " + temp_D_4);System.out.println("----------------------------------");//8 —— static double max(double x, double y) 和 min(double x, double y) : 获取两个数中的最大值和最小值System.out.println("134.23 和 111.11, 哪个数更大?" + Double.max(134.23, 111.11));System.out.println("222.111 和 111.222, 哪个数更小?" + Double.min(222.111, 111.222));System.out.println("----------------------------------");//9 —— static double sum(double x, double y) : 返回(x + y)的值System.out.println("11111.11 + 8889.022 = " + Double.sum(11111.11,8889.022));}
}
运行结果 :

8.Boolean类常用方法汇总 :
以Boolean_类为演示类,代码如下 :
package csdn.knowledge.api.Integer.Eight;public class Boolean_ {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 int 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 Short valueOf(...) : 字符串类型 ——> Boolean类型Boolean temp_B_4 = Boolean.valueOf("false");System.out.println("Boolean类型对象temp_B_4的值 = " + temp_B_4);}
}
运行结果 :

六、包装类总结 :
🆗,以上就是我们包装类的全部内容了。重点在于String类与基本类型/包装类型之间的相互转化。如何进行的转化,使用了什么方法,大家一定要烂熟于心。对于八大包装类的常用方法,虽然说肯定有遗漏的,没有举出例子的方法,而且up也并没有把各个方法的源码放出来,给大家仔细讲解源码。但是毕竟是基础阶段,委实不适合如此干。当然up自己是看过源码才知道这些个方法是咋用的,大家如果有兴趣,也很简单,直接Ctrl + b/B 追进去看看,或者Debug一下就懂了。至于这些个方法的用法,我觉得已经标注的挺清楚了。感谢阅读!
System.out.println("END--------------------------------------------------");
相关文章:

java 包装类 万字详解(通俗易懂)
前言简介和溯源拆装箱String类和基本类型的相互转化String类和包装类型的相互转化八大包装类的常用方法汇总(含代码演示)一、前言 : 本节内容是我们《API-常用类》专题的最后一节了。本节内容主要讲包装类,内容包括但不限于包装类的诞生&…...

为什么我复制的中文url粘贴出来会是乱码的? 浏览器url编码和解码
为什么我复制的中文url粘贴出来会是乱码的? 浏览器url编码和解码 Start 番茄最近涉及到一些和单点登录相关的业务需求,在实现功能的过程中,难免少不了和 url 打交道。但是在打交道的过程中,遇到一个痛点:明明我复制的…...

移动端适配
是看的b站一个老哥的视频,做的汇总,讲的嘎嘎棒。视频链接:b站链接 视口viewport pc端视口就是可视化的窗口,不包含浏览器工具栏但是移动端,不太一样,布局的视口和可见的视口是不太一样的 移动端的网页…...

【FPGA】Verilog:时序电路应用 | 序列发生器 | 序列检测器
前言:本章内容主要是演示Vivado下利用Verilog语言进行电路设计、仿真、综合和下载 示例:序列发生器与序列检测器 功能特性: 采用 Xilinx Artix-7 XC7A35T芯片 配置方式:USB-JTAG/SPI Flash 高达100MHz 的内部时钟速度 存储器…...

Biomod2 (下):物种分布模型建模
这里写目录标题1.给出一个线性回归模型并求出因子贡献度2.biomod22.1 pseudo-absences:伪不存在点(PA)2.1.1 random2.2.2 disk2.2.3 user.defined method3.使用网格划分区域3.1 计算质心4. 完整案例1.给出一个线性回归模型并求出因子贡献度 ##---------…...

Linux性能学习(2.2):内存_进程线程内存分配机制探究
文章目录1 进程内存分配探究1.1 代码1.2 试验过程2 线程内存分配探究2.1 代码2.2 试验过程3 总结参考资料:1. 嵌入式软件开发杂谈(3):Linux下内存与虚拟内存2. 嵌入式软件开发杂谈(1):Linux下最…...

BPMN2.0规范及流程引擎选型方案
BPMN2.0规范及流程引擎选型方案一、基本概念二、BPMN意义三、主要元素3.1 活动任务子流程调用活动事件子流程事务3.2 网关排他网关包容网关并行网关事件网关3.3 事件开始事件结束事件中间事件3.4 辅助泳道图注释与组数据存储四、图类型4.1 编排图4.2 会话图五、技术选型5.1 前端…...

VMware虚拟机安装Linux教程
前言 本文小新为大家带来 VMware虚拟机安装Linux教程 ,后边将为大家分享Linux系统的相关知识与操作,在此之前的第一步我们需要在我们的电脑上搭建好一个Linux系统的环境,本文的具体内容包括VMware虚拟机软件安装与Linux系统安装~ 不积跬步&a…...

多人协作|RecyclerView列表模块新架构设计
多人协作|RecyclerView列表模块新架构设计多人协作设计图新架构设计与实现设计背景与新需求新架构设计多人协作设计图 根据产品设计,将首页列表即将展示内容区域,以模块划分成多个。令团队开发成员分别承接不同模块进行开发,且互不影响任务开…...
SpringBoot (六) 整合配置文件 @Value、ConfigurationProperties
哈喽,大家好,我是有勇气的牛排(全网同名)🐮🐮🐮 有问题的小伙伴欢迎在文末评论,点赞、收藏是对我最大的支持!!!。 1 使用 Value 注解 /** Auth…...

docker 入门篇
docker为什么会出现? 一款产品:开发---->运维,两套环境!应用环境,应用配置! 常见问题:我的电脑可以运行,版本更新,导致服务不可用。 环境配置十分的麻烦,…...
MapReduce的shuffle过程详解
shuffle流程概括 因为频繁的磁盘I/O操作会严重的降低效率,因此“中间结果”不会立马写入磁盘,而是优先存储到Map节点的“环形内存缓冲区”,在写入的过程中进行分区(partition),也就是对于每个键值对来说&a…...

【软件使用】MarkText下载安装与汉化设置 (markdown快捷键收藏)
一、安装与汉化 对版本没要求的可以直接选择 3、免安装的汉化包 1、下载安装MarkText MaxText win64 https://github.com/marktext/marktext/releases/download/v0.17.1/marktext-setup.exe 使用迅雷可以快速下载 2. 配置中文语言包 中文包下载地址:GitHub - chi…...
LeetCode笔记:Biweekly Contest 99
LeetCode笔记:Biweekly Contest 99 1. 题目一 1. 解题思路2. 代码实现 2. 题目二 1. 解题思路2. 代码实现 3. 题目三 1. 解题思路2. 代码实现 4. 题目四 1. 解题思路2. 代码实现 比赛链接:https://leetcode.com/contest/biweekly-contest-99 1. 题目一…...
初探富文本之CRDT协同实例
初探富文本之CRDT协同实例 在前边初探富文本之CRDT协同算法一文中我们探讨了为什么需要协同、分布式的最终一致性理论、偏序集与半格的概念、为什么需要有偏序关系、如何通过数据结构避免冲突、分布式系统如何进行同步调度等等,这些属于完成协同所需要了解的基础知…...

团队死气沉沉?10种玩法激活你的项目团队拥有超强凝聚力
作为项目经理和PMO,以及管理者最头疼的是团队的氛围和凝聚力,经常会发现团队死气沉沉,默不作声,你想尽办法也不能激活团队,也很难凝聚团队。这样的项目团队你很难带领大家去打胜仗,攻克堡垒。但是如何才能避…...
Spring三级缓存核心思想
spring在启动时候,会创建bean,并给bean填充属性,这事会使用到三级缓存 private final Map<String, Object> singletonObjects new ConcurrentHashMap<>(256); //一级缓存private final Map<String, Object> earlySingleto…...

深度学习算法训练和部署流程介绍--让初学者一篇文章彻底理解算法训练和部署流程
目录 1 什么是深度学习算法 2 算法训练 2.1 训练的原理 2.2 名词解释 3 算法C部署 3.1 嵌入式终端板子部署 3.3.1 tpu npu推理 3.3.2 cpu推理 3.2 服务器部署 3.2.1 智能推理 3.2.2 CPU推理 1 什么是深度学习算法 这里不去写复杂的概念,就用通俗的话说…...

计算机网络整理
TCP与UDP 介绍 HTTP:(HyperText Transport Protocol)是超文本传输协议的缩写,它用于传送WWW方式的数据,关于HTTP协议的详细内容请参考RFC2616。HTTP协议采用了请求/响应模型。 TCP:(Transmission Contro…...

闲人闲谈PS之三十八——混合制生产下WBS-BOM价格发布增强
惯例闲话:最近中《三体》的毒很深,可能是电视剧版确实给闲人这种原著粉带来太多的感动,又一次引发了怀旧的热潮,《我的三体-罗辑传》是每天睡前必刷的视频,结尾BGM太燃了。闲人对其中一句台词感触很深——人类不感谢罗…...

大数据学习栈记——Neo4j的安装与使用
本文介绍图数据库Neofj的安装与使用,操作系统:Ubuntu24.04,Neofj版本:2025.04.0。 Apt安装 Neofj可以进行官网安装:Neo4j Deployment Center - Graph Database & Analytics 我这里安装是添加软件源的方法 最新版…...
R语言AI模型部署方案:精准离线运行详解
R语言AI模型部署方案:精准离线运行详解 一、项目概述 本文将构建一个完整的R语言AI部署解决方案,实现鸢尾花分类模型的训练、保存、离线部署和预测功能。核心特点: 100%离线运行能力自包含环境依赖生产级错误处理跨平台兼容性模型版本管理# 文件结构说明 Iris_AI_Deployme…...

React第五十七节 Router中RouterProvider使用详解及注意事项
前言 在 React Router v6.4 中,RouterProvider 是一个核心组件,用于提供基于数据路由(data routers)的新型路由方案。 它替代了传统的 <BrowserRouter>,支持更强大的数据加载和操作功能(如 loader 和…...
day52 ResNet18 CBAM
在深度学习的旅程中,我们不断探索如何提升模型的性能。今天,我将分享我在 ResNet18 模型中插入 CBAM(Convolutional Block Attention Module)模块,并采用分阶段微调策略的实践过程。通过这个过程,我不仅提升…...
python如何将word的doc另存为docx
将 DOCX 文件另存为 DOCX 格式(Python 实现) 在 Python 中,你可以使用 python-docx 库来操作 Word 文档。不过需要注意的是,.doc 是旧的 Word 格式,而 .docx 是新的基于 XML 的格式。python-docx 只能处理 .docx 格式…...
css的定位(position)详解:相对定位 绝对定位 固定定位
在 CSS 中,元素的定位通过 position 属性控制,共有 5 种定位模式:static(静态定位)、relative(相对定位)、absolute(绝对定位)、fixed(固定定位)和…...

第 86 场周赛:矩阵中的幻方、钥匙和房间、将数组拆分成斐波那契序列、猜猜这个单词
Q1、[中等] 矩阵中的幻方 1、题目描述 3 x 3 的幻方是一个填充有 从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等。 给定一个由整数组成的row x col 的 grid,其中有多少个 3 3 的 “幻方” 子矩阵&am…...

pikachu靶场通关笔记22-1 SQL注入05-1-insert注入(报错法)
目录 一、SQL注入 二、insert注入 三、报错型注入 四、updatexml函数 五、源码审计 六、insert渗透实战 1、渗透准备 2、获取数据库名database 3、获取表名table 4、获取列名column 5、获取字段 本系列为通过《pikachu靶场通关笔记》的SQL注入关卡(共10关࿰…...

什么是Ansible Jinja2
理解 Ansible Jinja2 模板 Ansible 是一款功能强大的开源自动化工具,可让您无缝地管理和配置系统。Ansible 的一大亮点是它使用 Jinja2 模板,允许您根据变量数据动态生成文件、配置设置和脚本。本文将向您介绍 Ansible 中的 Jinja2 模板,并通…...

Mac下Android Studio扫描根目录卡死问题记录
环境信息 操作系统: macOS 15.5 (Apple M2芯片)Android Studio版本: Meerkat Feature Drop | 2024.3.2 Patch 1 (Build #AI-243.26053.27.2432.13536105, 2025年5月22日构建) 问题现象 在项目开发过程中,提示一个依赖外部头文件的cpp源文件需要同步,点…...