本系列博客汇总在这里:Java系列_汇总
目录
- 一、包装类概述
- 1、包装类
- 2、Byte 构造方法
- 3、Integer 构造方法
- 二、自动装箱、拆箱和包装类默认值问题
- 三、Integer 与 int 之间的区别(面试题)
一、包装类概述
1、包装类
把基本数据类型转换成类。我们使用基本数据类型做进制转换很麻烦,对于临界值也不好判断,包装类提供了很多方法供我们使用,这样会方便很多。
Integer 类在对象中包装了一个基本类型 int 的值。
2、Byte 构造方法
3、Integer 构造方法
- Integer 部分属性
- 示例
-
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);
}
}
二、自动装箱、拆箱和包装类默认值问题
- 基本数据类型 —> 包装类 == 装箱( JDK1.5 以后的特性,自动完成)
- 包装类 —> 基本数据类型 == 拆箱( JDK1.5 以后的特性,自动完成)
- 装箱和拆箱是不需要我们主动调用的,是 jvm 自动给我们完成的。建议在实际做项目时都使用 Integer 数据类型。
- 示例
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 之间的区别(面试题)
- int 是基本数据类型。Integer 是包装类,包装类中提供很多对整数的操作方法,int 和 Integer 之间可以自动装箱和拆箱。
- int 默认值为 0,Integer 默认值为 null。
- 示例
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;
}
}
如有错误,欢迎指正!