Java包装类转化

public static void main(String[] args) {
		// TODO Auto-generated method stub
		//装箱:把基本数据类型转换成包装类
		//1、自动装箱
		int t1 = 2;
		Integer t2 = t1;
		//2、手动装箱
		Integer t3 = new Integer(t1);

		//测试
		System.out.println("int类型变量t1="+t1);
		System.out.println("Integer类型对象t2="+t2);
		System.out.println("Integer类型对象t3="+t3);
		System.out.println("*************************");
		//拆箱:把包装类转换成基本数据类型
		//1、自动拆箱
		int t4 = t2;
		//2、手动拆箱
		int t5 = t2.intValue();
		//测试
		System.out.println("Integer类型对象t2="+t2);
		System.out.println("自动拆箱后,int类型变量t4="+t4);
		System.out.println("手动拆箱后,int类型变量t5="+t5);
		double t6=t2.doubleValue();
		System.out.println("手动拆箱后,double类型变量t6="+t6);

	}

 

public static void main(String[] args) {
		// TODO Auto-generated method stub
		//基本数据类型转换为字符串
		int t1 = 2;
		String t1 = Integer.toString(t1);
		//测试
		System.out.println("int类型转换为String类型对象t2="+t2);
		System.out.println("********************************");
		//字符串转换为基本数据类型
		//1、包装类的parse
		int t3 = Integer.praseInt(t2);
		//2、包装类的valueOf 先将字符串转换为包装类,再通过自动拆箱完成基本类型转换
		int t4 = Integer.valueOf(t2);
		//测试
		System.out.println("String类型转换为int类型变量t3="+t3);
		System.out.println("String类型转换为int类型变量t4="+t4);


	}

 

 

public static void main(String[] args) {
		// TODO Auto-generated method stub
		Integer one=new Integer(100);
		Integer two=new Integer(100);
                //one和two指向的是堆区里两个装着100数值的空间
		System.out.println("one==two的结果:"+(one==two));//false
		
		Integer three=100;//自动装箱
                //自动执行
		//Integer three=Integer.valueOf(100);
		System.out.println("three==100的结果:"+(three==100));//true
		
		//Integer four=100;
                //-128<参数<127也就是three在缓存区(对象池)里已经建立了一个对象,four在执行时
                //会先去缓存区(对象池)找是否存在这样一个对象,结果找到了three创建的,
                //所以他们指向的是同一个空间
		Integer four=Integer.valueOf(100);
		System.out.println("three==four的结果:"+(three==four));//true
		
		Integer five=200;
		System.out.println("five==200的结果:"+(five==200));//true
		
		Integer six=200;
                //数值超过我们在缓存区(对象池)里创建对象的范围
		System.out.println("five==six的结果:"+(five==six));//false
		
		Double d1=Double.valueOf(100);
		System.out.println("d1==100的结果:"+(d1==100));//true
		
		Double d2=Double.valueOf(100);
		System.out.println("d1==d2的结果:"+(d1==d2));//false
	}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值