在 java 开发中,包装类和异常处理是两个核心且高频的知识点。它们分别解决了基本数据类型的对象化问题,以及程序运行时的错误处理问题。
本文将结合代码示例,深入解析这两个概念,帮助你更好地理解和运用。
一、包装类:让基本数据类型 “对象化”
1. 什么是包装类?
java 中的数据类型从本质上分为两类:
- 基本数据类型:
byte,short,int,long,float,double,char,boolean。它们不是对象,在面向对象编程的某些场景(如集合操作)中会受到限制。 - 引用类型:通过
new关键字创建的对象。
为了让基本数据类型也能以对象的形式存在,java 提供了一组包装类,专门用来创建与 8 种基本数据类型对应的对象。
2. 基本数据类型与包装类的对应关系
| 基本数据类型 | 包装类 |
|---|---|
| byte | byte |
| short | short |
| int | integer |
| long | long |
| float | float |
| double | double |
| char | character |
| boolean | boolean |
这些包装类全部存放于 java.lang 包中,它们的继承关系如下:
- 一级父类:
object - 二级父类:
character,number,boolean number的子类:byte,short,integer,long,float,double
public class test {
public static void main(string[] args) {
byte b = 1;
byte byt = new byte(b);
short s = 2;
short shor = new short(s);
int i = 3;
integer integer = new integer(i);
long l = 4;
long lon = new long(l);
float f = 5.5f;
float flo = new float(f);
double d = 6.6;
double dou = new double(d);
char cha = 'j';
character charac = new character(cha);
boolean bo = true;
boolean bool = new boolean(bo);
}
}注意:从 java 5 开始,引入了自动装箱(autoboxing)功能,编译器会自动完成这个转换,例如 integer i = 3;。
拆箱:包装类 → 基本数据类型
将包装类对象转换回对应的基本数据类型。
public class test {
public static void main(string[] args) {
byte b = 1;
byte byt = new byte(b);
byte b1 = byt.bytevalue();
short s = 2;
short shor = new short(s);
short i1 = shor.shortvalue();
int i = 3;
integer integer = new integer(i);
int i2 = integer.intvalue();
long l = 4;
long lon = new long(l);
long l1 = lon.longvalue();
float f = 5.5f;
float flo = new float(f);
float v = flo.floatvalue();
double d = 6.6;
double dou = new double(d);
double v1 = dou.doublevalue();
char cha = 'j';
character charac = new character(cha);
char c = charac.charvalue();
boolean bo = true;
boolean bool = new boolean(bo);
boolean b2 = bool.booleanvalue();
}
}注意:同样,java 5 也引入了自动拆箱(unboxing)功能,例如 int i = new integer(3);。
二、异常处理:让程序 “优雅” 地应对错误
1. 什么是异常?
java 中的错误可以分为两大类:
- 编译时错误:一般指语法错误,编译器会在编译阶段就提示,无法生成
.class文件。 - 运行时错误:语法没有问题,可以正常通过编译,但在运行时报错。
异常就是 java 提供的一套机制,用来专门处理各种运行时错误。它会将具体的错误信息以及出错位置统一告知程序员,帮助我们快速定位和解决问题。
2. 常见的异常类型
java 内置了丰富的异常类,以下是几个最常见的:
arithmeticexception:数学异常,如除数为零。
system.out.println(10 / 0); // 抛出此异常
classnotfoundexception:类未定义异常,当尝试加载一个不存在的类时抛出。
system.out.println(class.forname("test2"));illegalargumentexception:参数格式异常,当传递了不合法或不恰当的参数时抛出。
public void test(integer integer) {
system.out.println(integer);
}
// 当通过反射调用此方法并传入字符串 "1" 时会抛出此异常arrayindexoutofboundsexception:数组下标越界异常。
int[] array = {1, 2, 3};
system.out.println(array[3]); // 数组长度为3,索引最大为2nullpointerexception:空指针异常,当调用一个null对象的方法或属性时抛出。
integer num = null; system.out.println(num.equals(1));
nosuchmethodexception:方法未定义异常,当尝试调用一个不存在的方法时抛出。numberformatexception:将其他数据类型转换为数值类型时的不匹配异常。
integer integer = new integer("a"); // 字符串 "a" 无法转换为整数3. 异常的使用:try-catch-finally
java 提供了 try-catch-finally 语句块来捕获和处理异常。
try:监听可能会抛出异常的代码。一旦出现错误,jdk 会自动创建一个错误对象(异常对象)。catch:用来捕获 jdk 创建的异常对象,并进行后续的处理。finally:无论程序是否抛出异常,finally代码块中的程序一定会执行(try代码块里),通常用于释放资源。
public class test3 {
public static void test3(string str) {
integer integer = null;
try {
integer = integer.valueof(str);
} catch (exception e) {
// 打印异常信息
system.out.println(e.getmessage());
}
}
}public class test {
public static void main(string[] args) throws exception {
system.out.println(test());
}
public static int test() {
try {
system.out.println("try");
return 10;
} catch (exception e) {
e.printstacktrace();
} finally {
system.out.println("finally...");
return 20;
}
}
}
// 输出:
// try
// finally...
// 20注意:如果 try 或 catch 中有 return 语句,finally 块仍然会在方法返回前执行。如果 finally 中也有 return,则会覆盖 try 或 catch 中的返回值。
4.throw和throws:主动抛出异常
throw 和 throws 是 java 在处理异常时使用的关键字,都用来抛出异常,但是使用方式和表示的含义完全不同。
throw:开发者主动创建一个异常对象并抛出,用于在代码逻辑中主动发现并报告错误。
public class test {
public static void main(string[] args) throws exception {
string str = "java";
if (str.equals("java")) {
// 主动创建并抛出一个 numberformatexception
throw new numberformatexception();
} else {
int num = integer.parseint(str);
}
}
}throws:标注在方法声明上,用来描述该方法可能会抛出的异常。它告诉调用者,调用此方法时需要处理这些可能的异常。
public class test {
public static void main(string[] args) throws exception {
try {
test();
} catch (numberformatexception e) {
e.printstacktrace();
}
}
// 声明此方法可能会抛出 numberformatexception
public static void test() throws numberformatexception {
string str = "java";
int num = integer.parseint(str);
}
}5. 自定义异常
当内置的异常类无法满足业务需求时,我们可以创建自定义异常。自定义异常类需要继承自 exception 类(或其子类)。
示例:定义一个方法,对传入的参数进行 ++ 操作并返回结果,同时要求参数传入的必须是整数类型,如果不是整数类型则抛出自定义异常。
// 1. 定义自定义异常类,继承 exception
public class numberexception extends exception {
public numberexception(string message) {
super(message);
}
}
// 2. 使用自定义异常
public class test {
public static void main(string[] args) {
test test = new test();
try {
int add = test.add("a"); // 传入字符串,不是整数
system.out.println(add);
} catch (numberexception e) {
// 捕获并处理自定义异常
e.printstacktrace();
}
}
// 方法声明可能抛出 numberexception
public int add(object object) throws numberexception {
if (!(object instanceof integer)) {
// 主动抛出自定义异常
throw new numberexception("传入的参数不是整数类型");
} else {
int num = (int) object;
return ++num;
}
}
}总结
- 包装类是基本数据类型的对象表示,解决了基本数据类型在面向对象编程中的局限性。装箱和拆箱是它们之间转换的核心操作。
- 异常处理是 java 提供的强大机制,通过
try-catch-finally、throw、throws以及自定义异常,我们可以编写出更健壮、更易于调试的代码。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论