包装类自动装箱,自动拆箱,包装类基本操作。

包装类自动装箱案例

package com;

/*
* 总结:
* 1.包装类已经放弃new了, 自动new,自动new,自动new。除非是相同类型转换int变为int
* 2.包装类的==是地址判断。对象之间==是值判断,也是地址判断
* 3.自动装箱,基础数据类型类型自动转换为包装类案例
* 4.基本数据类型和String之间转换。
*       整型进制问题。
*
* */

import jdk.internal.math.FloatingDecimal;//用于导入字符串变为浮点型

//基础数据类型包装类和String类对比
public class Test {
    public static void test1(){
        //声明,初始化
        //初始化为null
//        Integer myInt = null;
//        Void myVoid = null;
//        Character myChar = null;
//        Double myDouble = null;
//        Float myFloat = null;
//        Byte myByte = null;
//        Short myShort = null;
//        Boolean myBoolean = null;

//        Integer myInt = new Integer(1);//不推荐
//        Integer myInt = new Integer("-1");//不推荐
//        Void myVoid = new Void();无法实例化void类

//        Character myChar= new Character('1');//char字符

//        Double mydouble1 = new Double("3.14");
//        Double mydouble2 = new Double(3.14);

//        Byte myByte1 = new Byte("123");
//        Byte myByte2= new Byte((byte)123);
//        Short myShort1 = new Short((short)666);
//        Short myShort2 = new Short("666");
//        Float myFlaot1 = new Float(3.14);
//        Float myFlaot2 = new Float(3.14f);
//        Float myFlaot3 = new Float("3.14");
//        Long myLong1 = new Long("2011111111111111111");
//        Long myLong2 = new Long(2011111111111111111L);

//        Boolean myBoolean1 = new Boolean(true);
//        Boolean myBoolean2 = new Boolean("true");
    }

    //参数赋值和==判断
    public static void test2(){
        // ==判断的是地址
        // =作用
        // 1.只能将地址赋值,
        // 2.不能改变原来自己地址内的值,不影响
        // 3.同一类,或者子类(实现类),null可以赋值,
        // 4.不同类之间不能赋值,因为实例化时就没有实现

//        Byte myByte1 = 12;
//        Byte myByte2 = 12;
//        System.out.println(myByte1==myByte2);//true

//        Byte myByte3 = new Byte((byte)12);
//        Byte myByte4 = new Byte((byte)12);
//        System.out.println(myByte3==myByte4);//false

//        Integer myInt1 = 19;
//        Integer myInt2 = myInt1;
//        myInt2 = 20;//重新分配了一个地址
//        System.out.println(myInt1);//19

        String str1 = new String("123");
        String str2 = new String("123");
        System.out.println(str1==str2);//false

        str1 = str2;
        str1 = "赵六";
        System.out.println(str2);//123
        str1 = str2;
        str2 = "王五";
        System.out.println(str1);//123

        str1 = "1223";
        str2 = "1223";
        System.out.println(str1==str2);//true

    }

    //值判断 equals
    public static void test3(){
        Short myShort1 = 12;
        Short myShort2 = new Short((short)12);
        Short myShort3 = new Short("10");
        System.out.println(myShort1.equals(myShort2));//true
        System.out.println(myShort2.equals(myShort3));//false
     }
     //自动装箱,自动拆箱
    //赋值只有同一类型才能赋值,但是基础数据类型和包装类之间可以湘湖赋值
    //但是基础数据类型本身的类型没有变,只是值变了。 所以基础数据类型不能泛型,int[]也不能变为Integer[]
    //自动装箱包装类是用的基础数据类型的地址,地址拷贝
    //自动拆箱是基础数据类型的值拷贝
    public static void test4(){
        //自动拆箱
        short myShort1 = new Short((short)12);
        myShort1 = new Short("10");
        //自动装箱
        Short myShosrt2 = myShort1;
        myShosrt2 = 100;
        Short myShort3 = 100;
        System.out.println(myShosrt2==myShort3);//true
        myShosrt2 = myShort1;
        //自动拆箱
        myShort1 = myShosrt2;
    }

    //包装类和String的转换
    //String变为包装类
    //包装类变为字符串
    public static void test5(){
        //其它类型变为字符串,
        // 基本数据类型,包装类不同
        // 1.基本数据类型, Integer.toString(i)
        // 2.包装类, return (obj == null) ? "null" : obj.toString();
        // 3.包装类的方法参数都是是Object,
        // 总结:其实能直接最好

        String str = String.valueOf(123);
//        System.out.println(str);//123

        str = String.valueOf(new Integer(12));//对象

        //直接调用
        str = Integer.toString(123);
        str = new Integer(12).toString();//这里要防止不是null

        //字符串变为其它类型
        //后面那个是值String是几进制,返回是一个10进制
        int myint1 = Integer.parseInt(str,10);//10进制12变为10进制返回
        int myint2 = Integer.parseInt(str,8);//8进制12变为10进制返回
        Integer myint3 = Integer.parseInt(str,16);//自动打包
        System.out.println(myint1+" "+myint2+" "+myint3);

        double mydouble1 = Double.parseDouble(str);//FloatingDecimal.parseDouble(s)
        double mydouble2 = FloatingDecimal.parseDouble(str);//需要软件包

        Long myLong1 = Long.parseLong(str,10);
        byte mybyte = Byte.parseByte(str,10);
        short myShort = Short.parseShort(str,10);
        float myFloat = Float.parseFloat(str);
        myFloat = FloatingDecimal.parseFloat(str);
    }

    public static void main(String[] args){
            test5();
    }
}

 

1.自动装箱,自动拆箱是一种机制,可以理解就是默认的,就行字符串常量自动变为String一样,自动装箱也有点像自动类型转换。但是比自动类型转换更容易记忆。

 

posted @ 2022-06-19 10:30  liu/564  阅读(70)  评论(0)    收藏  举报