专注于 JetBrains IDEA 全家桶,永久激活,教程
持续更新 PyCharm,IDEA,WebStorm,PhpStorm,DataGrip,RubyMine,CLion,AppCode 永久激活教程

Java常用类-包装类

一、 包装类的使用

  • java提供了8种基本数据类型对应的包装类,使得基本数据类型的变量具有类的特征
  • 需要掌握的:基本数据类型、包装类、String三者之间的相互转换
  • 基本数据类型 《—————》包装类:自动装箱,自动拆箱
  • 基本数据类型、包装类—>String类型:调用String重载的valueOf(Xxx xxx)
  • String类型 —>基本数据类型、包装类:调用包装类的parseXxx(String s)
import org.junit.Test;
public class WrapperTest {

    //String类型 --->基本数据类型、包装类:调用包装类的parseXxx(String s)
    @Test
    public void test5(){
        String str1 = "123";
        //错误的情况:
//      int num1 = (int)str1;
//      Integer in1 = (Integer)str1;
        //可能会报NumberFormatException
        int num2 = Integer.parseInt(str1);
        System.out.println(num2 + 1);

        String str2 = "true1";
        boolean b1 = Boolean.parseBoolean(str2);
        System.out.println(b1);
    }

    //基本数据类型、包装类--->String类型:调用String重载的valueOf(Xxx xxx)
    @Test
    public void test4(){

        int num1 = 10;
        //方式1:连接运算
        String str1 = num1 + "";
        //方式2:调用String的valueOf(Xxx xxx)
        float f1 = 12.3f;
        String str2 = String.valueOf(f1);//"12.3"

        Double d1 = new Double(12.4);
        String str3 = String.valueOf(d1);
        System.out.println(str2);
        System.out.println(str3);//"12.4"

    }

    /*
     * JDK 5.0 新特性:自动装箱 与自动拆箱
     */
    @Test
    public void test3(){
//      int num1 = 10;
//      //基本数据类型-->包装类的对象
//      method(num1);

        //自动装箱:基本数据类型 --->包装类
        int num2 = 10;
        Integer in1 = num2;//自动装箱

        boolean b1 = true;
        Boolean b2 = b1;//自动装箱

        //自动拆箱:包装类--->基本数据类型
        System.out.println(in1.toString());

        int num3 = in1;//自动拆箱

    }

    public void method(Object obj){
        System.out.println(obj);
    }

    //包装类--->基本数据类型:调用包装类Xxx的xxxValue()
    @Test
    public void test2(){
        Integer in1 = new Integer(12);

        int i1 = in1.intValue();
        System.out.println(i1 + 1);

        Float f1 = new Float(12.3);
        float f2 = f1.floatValue();
        System.out.println(f2 + 1);
    }

    //基本数据类型 --->包装类:调用包装类的构造器
    @Test
    public void test1(){

        int num1 = 10;
//      System.out.println(num1.toString());
        Integer in1 = new Integer(num1);
        System.out.println(in1.toString());

        Integer in2 = new Integer("123");
        System.out.println(in2.toString());

        //报异常
//      Integer in3 = new Integer("123abc");
//      System.out.println(in3.toString());

        Float f1 = new Float(12.3f);
        Float f2 = new Float("12.3");
        System.out.println(f1);
        System.out.println(f2);

        Boolean b1 = new Boolean(true);
        Boolean b2 = new Boolean("TrUe");
        System.out.println(b2);
        Boolean b3 = new Boolean("true123");
        System.out.println(b3);//false

        Order order = new Order();
        System.out.println(order.isMale);//false
        System.out.println(order.isFemale);//null
    }

}

class Order{

    boolean isMale;
    Boolean isFemale;
}

103_1.png

二、问题

103_2.png

public void test() {
        Object o1 = true ? new Integer(1) : new Double(2.0);
        //默认调用toString方法
        System.out.println(o1);//1.0 
        System.out.println(o1.toString());// 1.0
   }
/*三目运算符比较基本数据类型,所以在编译阶段自动拆箱为 int 和 double 类型,由于三目运算符要求 表达式2 和 表达式3 类型一致,所以在编译阶段自动类型提升(即 int 自动类型转换为 double 类型),再自动装箱为Object,输出时使用多态调用重写的toString();即Double包装类的toString();
其实这里面包含一个三元一次运算符和一个赋值运算,因为三目运算符的优先级高于赋值运算符,所以先运行三目运算符
*/

103_3.png

@Test
    public void test3() {
        Integer i = new Integer(1);
        Integer j = new Integer(1);
        System.out.println(i == j);//false

        //Integer内部定义了IntegerCache结构,IntegerCache中定义了Integer[],
        //保存了从-128~127范围的整数。如果我们使用自动装箱的方式,给Integer赋值的范围在
        //-128~127范围内时,可以直接使用数组中的元素,不用再去new了。目的:提高效率

        Integer m = 1;
        Integer n = 1;
        System.out.println(m == n);//true

        Integer x = 128;//相当于new了一个Integer对象
        Integer y = 128;//相当于new了一个Integer对象
        System.out.println(x == y);//false

文章永久链接:https://tech.souyunku.com/37086

未经允许不得转载:搜云库技术团队 » Java常用类-包装类

JetBrains 全家桶,激活、破解、教程

提供 JetBrains 全家桶激活码、注册码、破解补丁下载及详细激活教程,支持 IntelliJ IDEA、PyCharm、WebStorm 等工具的永久激活。无论是破解教程,还是最新激活码,均可免费获得,帮助开发者解决常见激活问题,确保轻松破解并快速使用 JetBrains 软件。获取免费的破解补丁和激活码,快速解决激活难题,全面覆盖 2024/2025 版本!

联系我们联系我们