欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

Java 学习(普通类)

最编程 2024-03-07 17:18:46
...

一、包装类(针对八种基本数据类型相应的引用类型--包装类.

1)包装类和基本数据类型的相互转换

装箱:基本类型->包装类型

拆箱:包装类型->基本类型

//以下是int类型和char类型演示。
public class temp1 {
    public static void main(String[] args) {
        //int<-->Integer的装箱(基本类型->包装类型)和拆箱(包装类型->基本类型)
        //1,手动装箱:
        int n=10;
        Integer integer =new Integer(n);
        //或Integer integer1=Integer.valueOf(n)

        //2.手动拆箱
        int i=integer.intValue();

        //jdk5后,就可以自动装箱和自动拆箱
        int a=20;
        char ch='g';
        //1.自动装箱
        Integer integer2=a;//底层使用的依然是Integer.valueOf(a)
        Character character=ch;
        //2.自动拆箱
        int b=integer2;
        char ch1=character;
    }
}

插入知识点:三元运算符

public class temp1 {
    public static void main(String[] args) {
        Object obj1=true?new Integer(1):new Double(2.0);
        System.out.println(obj1);//实际输出为1.0
        //因为三元运算符是一个整体,自动类型转换
    }
}

推荐阅读