Java自学之路:包装类

1.为什么要有包装类(或封装类)
为了使基本数据类型的变量具有类的特征,引入包装类。

2.基本数据类型与对应的包装类:
在这里插入图片描述

3.需要掌握的类型间的转换:(基本数据类型、包装类、String)
在这里插入图片描述

简易版:
基本数据类型<—>包装类:JDK 5.0 新特性:自动装箱 与自动拆箱

基本数据类型、包装类—>String:调用String重载的valueOf(Xxx xxx)

String—>基本数据类型、包装类:调用包装类的parseXxx(String s)
注意:转换时,可能会报NumberFormatException

应用场景举例:
① Vector类中关于添加元素,只定义了形参为Object类型的方法:
v.addElement(Object obj); //基本数据类型 —>包装类 —>使用多态

package 包装类的使用;

import org.junit.Test;

public class InterviewTest {

	
	@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
	}
	
}

package 包装类的使用;

import org.junit.Test;
/*
 * 包装类的使用:
 * 1. java提供了8种基本数据类型对应的包装类,使得基本数据类型的变量具有类的特征
 * 
 * 2.掌握的:基本数据类型、包装类、String三者之间的相互转换
 * 
 */
public class WrapperTest {
	
	//String类型--->基本数据类型、包装类:调用包装类的parseXxx()
	@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);
		
		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);
	}
	
	//包装类 ---> 基本数据类型:调用包装类的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 num = 10;
//		System.out.println(num.toString());
		Integer in1 = new Integer(num);
		System.out.println(in1.toString());
		
		Integer in2 = new Integer("123");
		System.out.println(in2.toString());
		
		//报异常
//		Integer in3 = new Integer("123aaa");
//		System.out.println(in3.toString());
		
		Float f1 = new Float(12.3);
		Float f2 = new Float("12.3");
		
		System.out.println(f1);
		System.out.println(f2);
		
		Boolean b1 = new Boolean(true);
		Boolean b2 = new Boolean("TrUe");//详细看Boolean的源码
		
		System.out.println(b2);
		
		Boolean b3 = new Boolean("true123");
		System.out.println(b3);//flase
		
		Order order = new Order();
		System.out.println(order.isMale);//false
		System.out.println(order.isFemale);//null
		
		
	}
}

class Order{
	boolean isMale;
	Boolean isFemale;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值