Java系列(37)——包装类

本系列博客汇总在这里:Java系列_汇总


一、包装类概述

1、包装类

把基本数据类型转换成类。我们使用基本数据类型做进制转换很麻烦,对于临界值也不好判断,包装类提供了很多方法供我们使用,这样会方便很多。
在这里插入图片描述
Integer 类在对象中包装了一个基本类型 int 的值。

2、Byte 构造方法

在这里插入图片描述

3、Integer 构造方法

在这里插入图片描述

  1. Integer 部分属性
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
  2. 示例
    package cn.tx;
    
    public class Demo
    {
    	public static void main(String[] args)
    	{
    		/**
    		 * 第一种构造器: Byte(byte value) 描述:构造一个新分配的 Byte对象,该对象表示指定的 byte值。 第二种构造器:
    		 * Byte(String s) 描述:构造一个新分配 Byte对象,表示 byte由指示值 String参数。
    		 */
    		// 两种构造器
    		byte a = 0;// byte 数据类型定义
    		Byte a1 = new Byte("0");// Byte 类创建对象
    		Byte a2 = new Byte(a);// Byte 类创建对象
    		System.out.println("a1:" + a1 + "\t" + "a2:" + a2);
    
    		/**
    		 * 第一种构造器: Integer(int value) 描述: 构造一个新分配的 Integer对象,该对象表示指定的 int值。 第二种构造器:
    		 * Integer(String s) 描述:构造一个新分配 Integer对象,表示 int由指示值 String参数。
    		 */
    		// 两种构造器
    		int b = 0;// int 数据类型定义
    		Integer b1 = new Integer("0");// Integer 类创建对象()
    		Integer b2 = new Integer(b);// Integer 类创建对象
    		System.out.println("b1:" + b1 + "\t" + "b2:" + b2);
    
    		// 通过属性获得int的最大值((2^31)-1)和最小值(-2^31)
    		System.out.println("int最大值:" + Integer.MAX_VALUE);
    		System.out.println("int最小值:" + Integer.MIN_VALUE);
    
    		/**
    		 * int intValue() 描述:将 Integer的值作为 int 。
    		 */
    		// int基本数据类型和Integer包装类转化
    		int c = 0;
    		// int-->integer
    		Integer c1 = new Integer(c);// 方法一
    		Integer c2 = Integer.valueOf(c);// 方法二
    		System.out.println("c1:" + c1 + "\t" + "c2:" + c2);
    		// integer-->int
    		int c3 = c1.intValue();
    		System.out.println("c3:" + c3);
    
    		/**
    		 * valueOf(String s) 描述:返回一个 Integer对象,保存指定的值为 String 。
    		 */
    		String d = "0";
    		// String-->Integer(使用较多)
    		// 注意:将String转为Integer,“*”当中字符串*必须是数值类型,以及保证字符串不是空。
    		Integer d1 = new Integer(d);// 方法一(推荐)
    		Integer d2 = Integer.valueOf(d);// 方法二
    		System.out.println("d1:" + d1 + "\t" + "d2:" + d2);
    
    		/**
    		 * parseInt(String s) 描述:将字符串参数解析为带符号的十进制整数。
    		 */
    		// String-->int
    		String e = "0";
    		int e1 = Integer.parseInt(e);
    		System.out.println("e1:" + e1);
    
    		/**
    		 * toString(int i) 描述:返回一个 String指定整数的 String对象。
    		 */
    		// Integer-->String
    		Integer f = new Integer(0);
    		String f1 = f.toString();// 方法一
    		String f2 = f1 + "";// 方法二(字符串与任何数据类型相加得到的都是字符串类型)
    		String f3 = String.valueOf(f);// 方法三(根据String类里的方法 value(Object obj)(Object是所有类的父类,当然可以将对象传过去))
    		System.out.println("f1:" + f1 + "\t" + "f2:" + f2 + "\t" + "f3:" + f3);
    	}
    }
    
    在这里插入图片描述

二、自动装箱、拆箱和包装类默认值问题

  1. 基本数据类型 —> 包装类 == 装箱( JDK1.5 以后的特性,自动完成)
  2. 包装类 —> 基本数据类型 == 拆箱( JDK1.5 以后的特性,自动完成)
  3. 装箱和拆箱是不需要我们主动调用的,是 jvm 自动给我们完成的。建议在实际做项目时都使用 Integer 数据类型。
  4. 示例
    package cn.tx;
    
    public class Demo
    {
    	public static void main(String[] args)
    	{
    
    		// 基本数据类型转化为包装类就是装箱(JDK1.5以后的特性,自动完成)
    		int i = 0;
    		// 装箱
    		Integer iClass = i;
    		System.out.println("iClass:" + iClass);
    
    		// 包装类转化为基本数据类型就是拆箱(JDK1.5以后的特性,自动完成)
    		Integer i1 = new Integer(0);
    		// 拆箱
    		int i2 = i1;
    		System.out.println("i2:" + i2);
    	}
    }
    
    在这里插入图片描述

三、Integer 与 int 之间的区别(面试题)

  1. int 是基本数据类型。Integer 是包装类,包装类中提供很多对整数的操作方法,int 和 Integer 之间可以自动装箱和拆箱。
  2. int 默认值为 0,Integer 默认值为 null。
  3. 示例
    package cn.tx;
    
    public class Demo
    {
    	public static void main(String[] args)
    	{
    
    		Person p = new Person();
    		// int默认值为0,Integer默认值为null。
    		System.out.println("int 默认值:" + p.getId());
    		System.out.println("Integer 默认值:" + p.getAge());
    
    		// 空值与值相加会报错(空指针异常)。
    		// Integer i = p.getAge() + 0;
    		// 所以对象在使用前要判断是否为空。
    	}
    }
    
    package cn.tx;
    
    public class Person
    {
    	private int id;
    	private Integer age;
    
    	public int getId()
    	{
    		return id;
    	}
    
    	public void setId(int id)
    	{
    		this.id = id;
    	}
    
    	public Integer getAge()
    	{
    		return age;
    	}
    
    	public void setAge(Integer age)
    	{
    		this.age = age;
    	}
    }
    
    在这里插入图片描述

如有错误,欢迎指正!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值