当前位置:首页 > 图灵资讯 > 技术篇> 包装类

包装类

发布时间:2023-05-15 09:34:21

1.包装分类459

1)对应于八种基本数据类型的引用类型-包装类型

2)有了类的特点,可以调用类中的方法。

3)如图所示,黄色底纹属于父类Number

包装类_自动装箱

包装类_包装类_02

包装类_自动装箱_03

包装类_System_04

com中的代码.stulzl.wrapper_type.Wrapper_Type
package com.stulzl.wrapper_type;///包装分类  459public class Wrapper_Type {    public static void main(String[] args) {        //基本数据类型->包装类        //boolean -> Boolean        //char -> Character        //byte -> Byte        //int -> Integer        //long -> long        //float -> Float        //double -> Double        //short -> Short    }}
2.转换包装类和基础数据460

以int和integer演示包装类和基本数据类型的相互转换。

1)jdk5前的手动装箱和拆箱方式,装箱:基本类型->相反,拆箱的包装类型

2)jdk5后(含jdk5)的自动装箱和拆箱方式

3)valueof法用于自动装箱底部,如Integerer.valueOf(

4)其他包装类别的用法相似,不一一举例

com中的代码.stulzl.wrapper_integer01.包中Integer01
package com.stulzl.wrapper_integer01;///转换包装类和基本数据 演示int--integer  460public class Integer01 {    public static void main(String[] args) {        //演示 int <--> Integer 包装和拆箱        //jdk5 前者是手动装箱和拆箱        ///手动装箱 int->Integer        int n1 = 100;        Integer integer = new Integer(n1);////手动装箱方法1        Integer integer1 = Integer.valueOf(n1);////手动装箱方法2        ///手动拆箱        //Integer -> int        int i = integer.intValue();        //jdk5 后,可自动装箱和自动拆箱        int n2 = 200;        //自动装箱int->integer        Integer integer2 = n2;//使用底层 Integer.valueOf(n2)        //Integer自动拆箱->int        int n3 = integer2;////底层仍在使用 intValue()方法        ///演示char-character类型的转换        char c1 = 'a';                ///手动装箱charar->Character        Character character = new Character(c1);        Character character2 = Character.valueOf(c1);        ///Character手动拆箱->char        char c = character.charValue();                //自动装箱charar->Character        Character character = c1;        ///Character自动拆箱->char        char c2 = character;            }}
3.包装练习461

包装类_包装类_05

com中的代码.stulzl.wrapper_exercise01.包中Exercise01
package com.stulzl.wrapper_exercise01;///小包装练习 判断输出461publiclic class Exercise01 {    public static void main(String[] args) {        Double d = 100d;//正确 自动装箱 Double.valueOf(100d);        Float f = 1.5f;//正确 自动装箱 Float.valueOf(1.5f);        Object obj1 = true? new Integer(1) : new Double(2.0);///三元运算符[是一个整体] 一真大师        //1.0//解释为什么是1.0而不是1,因为 ? :  三元运算符是一个整体 因为new Double(2.0)Double类型        //所以new Integer(1)将类型转换为Double        System.out.println(obj1);//1.0        Object obj2;        if(true)            obj2 = new Integer(1);        else            obj2 = new Double(2.0);        System.out.println(obj2);//1        ///输出什么 ? 1, 分别计算    }}
4.包装类型与String类型的相互转换462

以Integer和String转换为例,案例演示其他类似:

com中的代码.stulzl.wrapper_string.Wrapervstring包
package com.stulzl.wrapper_string;//包装类型和 String 相互转换类型  462//案例演示, 以 Integer 和 String 转换为例,其它类似:public class WrapperVSString {    public static void main(String[] args) {        ////包装Integer->String        Integer i = 100;        //方式1        String str1 = i+"";        //方式2        String s = i.toString();        //方式3        String s1 = String.valueOf(i);        //String->Integer包装        String str4 = "123";        //方法1        Integer i2 = Integer.parseInt(str4);        //方法2        Integer i4  = new Integer(str4);        System.out.println("ok~~~");    }}
5.Integer类和Character类的常用方法462代码在comm.stulzl.wrapper_method.Wrappermethod包
package com.stulzl.wrapper_method;//Integer 类和 Character 常用的类别方法  462public class Wrapermethod0 {    public static void main(String[] args) {        System.out.println(Integer.MIN_VALUE); //回到最小值        System.out.println(Integer.MAX_VALUE);//返回最大值        System.out.println(Character.isDigit('a'));//判断是否是数字        System.out.println(Character.isLetter('a'));//判断是否是字母        System.out.println(Character.isUpperCase('a'));//判断是否是大写        System.out.println(Character.isLowerCase('a'));//判断是否是小写        System.out.println(Character.isWhitespace('a'));//判断是否是空格        System.out.println(Character.toUpperCase('a'));//转大写        System.out.println(Character.toLowerCase('A'));///转小写        ///自己写        System.out.println(Integer.MAX_VALUE);        System.out.println(Integer.MIN_VALUE);        System.out.println(Character.isDigit('a'));        System.out.println(Character.isLetter('a'));        System.out.println(Character.isLowerCase('a'));        System.out.println(Character.isUpperCase('a'));        System.out.println(Character.isSpaceChar('a'));        System.out.println(Character.toLowerCase('a'));        System.out.println(Character.toUpperCase('a'));    }}
6.经典的Integer面试题 4636.1判断输出

代码在

package com.stulzl.wrapper_exercise02;//经典面试题  463public class Exercise02 {    public static void main(String[] args) {        Integer i = new Integer(1);        Integer j = new Integer(1);        System.out.println(i == j); //False//因为这里是直接new得到i和j的两个对象,对象必须不同        Integer m = 1; //底层 Integer.valueOf(1); -> 阅读源代码(原码有数值范围,new对象不需要在范围内)        Integer n = 1;//底层 Integer.valueOf(1);        System.out.println(m == n); //T ///这个问题在范围内        //所以,这里主要看范围 -128 ~ 127 也就是直接返回        /*        老韩解读        //1. 如果 i 在 IntegerCache.low(-128)~IntegerCache.high(127)直接从数组返回        //2. 如果不在 -128~127,就直接 new Integer(i)        原码        public static Integer valueOf(int i) {            if (i >= IntegerCache.low && i <= IntegerCache.high)                return IntegerCache.cache[i + (-IntegerCache.low)];            return new Integer(i);////返回超出范围的new对象        }        */        //所以,这里主要看范围 -128 ~ 127 也就是直接返回        /,否则,就 new Integer(xx);,这个问题显然不在范围内,直接返回new对象        Integer x = 128;//底层 Integer.valueOf(1);        Integer y = 128;//底层 Integer.valueOf(1);        System.out.println(x == y);//False    }}

包装类_System_06

上一篇 #yyds干货盘点# LeetCode程序员面试金典:二叉树的锯齿形层序遍历
下一篇 try-catch,throw和throws

文章素材均来源于网络,如有侵权,请联系管理员删除。

标签: Java教程Java基础Java编程技巧面试题Java面试题