Java API学习笔记
一.类
1. String 类
不可变性:String
对象创建后不可修改,每次操作返回新对象
String str = "Hello";
str.length();
str.charAt(0);
str.substring(1, 4);
str.indexOf("l");
str.equals("hello");
str.toUpperCase();
String.join("-", "a", "b", "c");
2. StringBuilder & StringBuffer
可变字符串:适合频繁修改字符串的场景
区别:
StringBuilder(非线程安全,性能高)
StringBuffer(线程安全,性能低)
StringBuilder sb = new StringBuilder("Java");sb.append(" API"); sb.insert(0, "Learn "); sb.reverse();
3. Wrapper 类
基本类型 → 对象:Integer、Double、Boolean等
自动装箱/拆箱:
Integer num = 10; int value = num;
常用方法:
Integer.parseInt("123"); Double.valueOf("3.14");
二、集合框架(java.util)
1. 核心接口
List:有序可重复,如ArrayList、LinkedList
Set:无序唯一,如HashSet、TreeSet
Map:键值对,如HashMap、TreeMap。
2. List示例
import java.util.ArrayList;
import java.util.List;
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.get(0);
list.size();
list.remove(0);
3. Map示例
import java.util.HashMap;
import java.util.Map;
Map<String, Integer> map = new HashMap<>();
map.put("one", 1);
map.put("two", 2);
map.get("one");
map.containsKey("two");
for (Map.Entry<String, Integer> entry : map.entrySet()) {System.out.println(entry.getKey() + ": " + entry.getValue());
}
4. 泛型
类型安全:指定集合存储的元素类型
List<Integer> numbers = new ArrayList<>();numbers.add(10);
三、日期时间处理(java.time)
1. LocalDate/LocalTime/LocalDateTime
import java.time.LocalDate;import java.time.LocalDateTime;LocalDate today = LocalDate.now();LocalDate birthday = LocalDate.of(2000, 1, 1);LocalDateTime now = LocalDateTime.now();
2. 格式化与解析
import java.time.format.DateTimeFormatter;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate date = LocalDate.parse("2025-05-18", formatter);
String formattedDate = date.format(formatter);
四、异常处理
1. 异常分类
受检异常(Checked Exception):必须显式处理,如IOException
非受检异常(Unchecked Exception):继承自RuntimeException,如NullPointerException
2. 捕获与抛出
try {int result = 10 / 0;
} catch (ArithmeticException e) {System.out.println("除数不能为0");
} finally {
}
class MyException extends Exception {public MyException(String message) {super(message);}
}
五、多线程
1. 创建线程
继承Thread类:
class MyThread extends Thread {public void run() {System.out.println("线程执行");}}MyThread thread = new MyThread();thread.start();
实现Runnable接口:
class MyRunnable implements Runnable {public void run() {System.out.println("任务执行");}}Thread thread = new Thread(new MyRunnable());thread.start();
2. 线程池
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
ExecutorService executor = Executors.newFixedThreadPool(5);
executor.submit(() -> {System.out.println("线程池任务");
});
executor.shutdown();
六、IO流
1. 文件读写
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
try (FileReader reader = new FileReader("test.txt")) {int data;while ((data = reader.read()) != -1) {System.out.print((char) data);}
} catch (IOException e) {e.printStackTrace();
}
try (FileWriter writer = new FileWriter("output.txt")) {writer.write("Hello, Java!");
} catch (IOException e) {e.printStackTrace();
}
2. NIO
import java.nio.file.Files;
import java.nio.file.Paths;
List<String> lines = Files.readAllLines(Paths.get("test.txt"));
七、反射
动态获取类信息:
Class<?> clazz = Class.forName("java.util.ArrayList");System.out.println(clazz.getName()); // "java.util.ArrayList"Object obj = clazz.getDeclaredConstructor().newInstance();
八、网络编程
1. TCP示例
服务器端:
try (ServerSocket serverSocket = new ServerSocket(8080)) {Socket socket = serverSocket.accept();} catch (IOException e) {e.printStackTrace();}
客户端:
try (Socket socket = new Socket("localhost", 8080)) {// 发送/接收数据} catch (IOException e) {e.printStackTrace();}
九、Lambda表达式
简化匿名类:
new Thread(new Runnable() {public void run() {System.out.println("传统线程");}}).start();new Thread(() -> System.out.println("Lambda线程")).start();
集合遍历:
List<String> list = List.of("a", "b", "c");list.forEach(item -> System.out.println(item));