Java 异常类、常用类、容器、泛型主要内容:

本文详细介绍了Java中的异常处理机制,包括捕获异常和自定义异常,以及包装类、String类(包括StringBuilder)、BigDecimal用于高精度计算,以及泛型和常用的容器类(List、Set、Map)的使用实例。
摘要由CSDN通过智能技术生成

Java 异常类、常用类、容器、泛型主要内容:

  1. 异常类

    • 捕获异常:使用 try-catch 块来捕获异常,处理可能发生的异常情况。
    • 抛出异常:使用 throw 关键字手动抛出异常。
    • 自定义异常类:可以通过继承 ExceptionRuntimeException 类来创建自定义异常类。
  2. 常用类

    • 包装类:用于将基本数据类型转换为对象,如 IntegerDouble 等。
    • String 类:用于表示字符串,提供了丰富的字符串操作方法。StringBufferStringBuilder 用于处理可变的字符串。
    • BigDecimal 类:用于高精度的十进制运算,避免浮点数运算精度丢失的问题。
  3. 容器

    • 泛型简介:泛型提供了编译时类型安全检查,可以在编译时捕获类型转换错误。
    • 列表(List):如 ArrayListLinkedList,用于存储有序的元素集合。
    • 集合(Set):如 HashSetTreeSet,用于存储不重复的元素集合。
    • 映射(Map):如 HashMapTreeMap,用于存储键值对。

下面是一个结合以上内容的 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 类以及泛型的使用。
详细介绍

  1. 捕获、抛出、自定义异常类

捕获异常:使用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);
    }
}
  1. 包装类、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
  1. 泛型简介、列表、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);
  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值