Java 异常类、常用类、容器、泛型主要内容:
-
异常类:
- 捕获异常:使用
try-catch
块来捕获异常,处理可能发生的异常情况。 - 抛出异常:使用
throw
关键字手动抛出异常。 - 自定义异常类:可以通过继承
Exception
或RuntimeException
类来创建自定义异常类。
- 捕获异常:使用
-
常用类:
- 包装类:用于将基本数据类型转换为对象,如
Integer
、Double
等。 - String 类:用于表示字符串,提供了丰富的字符串操作方法。
StringBuffer
和StringBuilder
用于处理可变的字符串。 - BigDecimal 类:用于高精度的十进制运算,避免浮点数运算精度丢失的问题。
- 包装类:用于将基本数据类型转换为对象,如
-
容器:
- 泛型简介:泛型提供了编译时类型安全检查,可以在编译时捕获类型转换错误。
- 列表(List):如
ArrayList
、LinkedList
,用于存储有序的元素集合。 - 集合(Set):如
HashSet
、TreeSet
,用于存储不重复的元素集合。 - 映射(Map):如
HashMap
、TreeMap
,用于存储键值对。
下面是一个结合以上内容的 Java 代码演示:
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Main {
public static void main(String[] args) {
// 捕获异常示例
try {
int result = divide(10, 0);
} catch (ArithmeticException e) {
System.out.println("除数不能为0");
}
// 抛出异常示例
try {
throwCustomException();
} catch (CustomException e) {
System.out.println(e.getMessage());
}
// 包装类示例
Integer num = 10;
System.out.println("包装类示例:" + num);
// String 类示例
String str = "Hello";
StringBuilder builder = new StringBuilder(str);
builder.append(", World!");
System.out.println("StringBuilder 示例:" + builder.toString());
// BigDecimal 类示例
BigDecimal bigDecimal1 = new BigDecimal("10.5");
BigDecimal bigDecimal2 = new BigDecimal("20.3");
BigDecimal sum = bigDecimal1.add(bigDecimal2);
System.out.println("BigDecimal 示例:" + sum);
// 泛型示例
List<String> list = new ArrayList<>();
list.add("Java");
list.add("Python");
System.out.println("List 示例:" + list);
Map<String, Integer> map = new HashMap<>();
map.put("A", 1);
map.put("B", 2);
System.out.println("Map 示例:" + map);
}
public static int divide(int dividend, int divisor) {
return dividend / divisor;
}
public static void throwCustomException() throws CustomException {
throw new CustomException("自定义异常");
}
static class CustomException extends Exception {
public CustomException(String message) {
super(message);
}
}
}
这段代码演示了异常处理、包装类、String 类、BigDecimal 类以及泛型的使用。
详细介绍
- 捕获、抛出、自定义异常类
捕获异常:使用try-catch语句捕获异常。
try {
// 可能抛出异常的代码
} catch (Exception e) {
// 处理异常的代码
}
抛出异常:使用throw关键字抛出异常。
public void method() throws Exception {
// 可能抛出异常的代码
throw new Exception("异常信息");
}
自定义异常类:继承Exception类或其子类,实现自定义异常。
public class MyException extends Exception {
public MyException(String message) {
super(message);
}
}
- 包装类、String(buffer、builder)、BigDecimal
包装类:将基本数据类型封装成对象。
Integer i = Integer.valueOf(10);
int num = i.intValue();
String(buffer、builder):StringBuffer用于多线程安全,StringBuilder用于单线程。
StringBuffer sb = new StringBuffer();
sb.append("hello");
sb.append("world");
String result = sb.toString();
StringBuilder sb2 = new StringBuilder();
sb2.append("hello");
sb2.append("world");
String result2 = sb2.toString();
BigDecimal:用于精确计算浮点数。
BigDecimal a = new BigDecimal("0.1");
BigDecimal b = new BigDecimal("0.2");
BigDecimal c = a.add(b); // 结果为0.3
- 泛型简介、列表、set、map
泛型:在编译时检查类型安全,避免类型转换错误。
List<String> list = new ArrayList<String>();
list.add("hello");
String str = list.get(0); // 不需要类型转换
列表:List接口及其实现类(如ArrayList、LinkedList)。
List<String> list = new ArrayList<String>();
list.add("hello");
list.add("world");
Set:Set接口及其实现类(如HashSet、TreeSet)。
Set<String> set = new HashSet<String>();
set.add("hello");
set.add("world");
Map:Map接口及其实现类(如HashMap、TreeMap)。
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("hello", 1);
map.put("world", 2);