docs: update docs/java.md (#990)

This commit is contained in:
zhangym
2025-07-28 16:09:46 +08:00
committed by GitHub
parent c0567c6abf
commit 2251b24c61

View File

@@ -80,8 +80,8 @@ for (char c: word.toCharArray()) {
```java
char[] chars = new char[10];
chars[0] = 'a'
chars[1] = 'b'
chars[0] = 'a';
chars[1] = 'b';
String[] letters = {"A", "B", "C"};
int[] mylist = {100, 200};
boolean[] answers = {true, false};
@@ -311,8 +311,9 @@ int[] a2 = {1, 2, 3};
int[] a3 = new int[]{1, 2, 3};
int[] a4 = new int[3];
a4[0] = 1;
a4[2] = 2;
a4[3] = 3; // 会出现索引越界异常
a4[1] = 2;
a4[2] = 3; // 正常赋值
a4[3] = 4; // 会出现索引越界异常 ArrayIndexOutOfBoundsException
```
### 修改 Modify
@@ -340,7 +341,7 @@ for (int i=0; i < arr.length; i++) {
```java
String[] arr = {"a", "b", "c"};
for (int a: arr) {
for (String a: arr) {
System.out.print(a + " ");
}
// 输出: a b c
@@ -598,7 +599,7 @@ public class Dog {
// 构造方法无无返回值,方法名与类名相同
// 一个类可以有多个构造方法
//Java 默认提供一个构造方法
// Java 默认提供一个无参构造方法(只有在没有显式定义任何构造方法时)
public Dog() {
}
@@ -640,7 +641,7 @@ myDog.Say();
### 对象的继承
```java
public Animal() {
public Animal {
public String name;
@@ -654,7 +655,7 @@ public Animal() {
}
// Dog类继承Animal类
public Dog extends Animal(){
public Dog extends Animal {
// 调用父类的构造函数
public Dog(String name){
super(name);
@@ -667,7 +668,7 @@ public Dog extends Animal(){
#### Java继承的特点
- 子类拥有父类的非private的属性、方法
- 子类拥有父类中可访问的属性和方法public、protected 以及同包访问权限的成员)
- 子类可以有自己的属性、方法
- 子类可以重写父类的方法
@@ -709,15 +710,16 @@ class Dog extends Animal {
```java
class Animal {
// 被重写者
void Say(String sentence) {
void say(String sentence) {
System.out.println("Animal say: " + sentence);
}
}
class Dog extends Animal {
// 重写者
// 重写者位于被重写者的子类
void Say(String sentence) { // 签名与被重写者相同
// 重写者位于被重写者的子类 重写一般需要添加 @Override 注解
@Override
void say(String sentence) { // 签名与被重写者相同
System.out.println("Dog say: " + sentence);
}
}
@@ -732,7 +734,7 @@ class Dog extends Animal {
public class Overloading {
public int test(){
System.out.println("test1");
return 1;
return 1;
}
public void test(int a) {
@@ -785,7 +787,7 @@ public abstract class Animal {
this.name = name;
}
public abstract void Say();
public abstract void say();
}
public class Dog extends Animal {
@@ -793,7 +795,7 @@ public class Dog extends Animal {
super(name);
}
public void Say(){
public void say(){
System.out.println("Dog");
}
@@ -807,7 +809,7 @@ public class Dog extends Animal {
与类的继承不同,接口可以继承多个接口
```java
[可见] interface 接口名称 [extends 其他接口名] {
[可见性修饰符] interface 接口名称 [extends 其他接口名列表] {
// 声明变量
// 抽象方法
}
@@ -879,7 +881,7 @@ public class ExtendsThread extends Thread {
```java
public static void main(String[] args) throws ExecutionException, InterruptedException {
new Thread(new RunnableThread()).start();
new ExtendsThread2().start();
new ExtendsThread().start();
FutureTask<Integer> integerFutureTask = new FutureTask<>(new CallableTask());
integerFutureTask.run();
}
@@ -891,7 +893,7 @@ public static void main(String[] args) throws ExecutionException, InterruptedExc
- corePoolSize: 核心线程数
- maximumPoolSize: 最大线程数
- keepAliveTime: 线程空闲时间
- timeUni: 线程空闲时间单位
- TimeUnit: 线程空闲时间单位
- workQueue: 线程等待队列
- threadFactory: 线程创建工厂
- handler: 拒绝策略
@@ -921,9 +923,13 @@ synchronized(obj) {
...
}
// (静态)方法
public synchronized
(static) void methodName() {
// 实例方法同步
public synchronized void methodName() {
...
}
// 静态方法同步
public static synchronized void methodName() {
...
}
```
@@ -997,7 +1003,7 @@ try{
condition.signal();
condition.signalAll();
} finally {
lock.unlock
lock.unlock();
}
// LockSupport,可以先unpark,后续park不会阻塞线程
@@ -1023,7 +1029,7 @@ Java 框架搜集
[CopyOnWriteArraySet](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CopyOnWriteArraySet.html) | Set | Y | _N_ | Y | _N_ | One `null`
[ConcurrentSkipListSet](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentSkipListSet.html) | Set | Y | Y | Y | _N_ | _N_
[HashMap](https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html) | Map | _N_ | _N_ | _N_ | _N (key)_ | One `null` _(key)_
[HashTable](https://docs.oracle.com/javase/8/docs/api/java/util/Hashtable.html) | Map | _N_ | _N_ | Y | _N (key)_ | _N (key)_
[Hashtable](https://docs.oracle.com/javase/8/docs/api/java/util/Hashtable.html) | Map | _N_ | _N_ | Y | _N (key)_ | _N (key)_
[LinkedHashMap](https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html) | Map | Y | _N_ | _N_ | _N (key)_ | One `null` _(key)_
[TreeMap](https://docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html) | Map | Y | Y | _N_ | _N (key)_ | _N (key)_
[ConcurrentHashMap](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentHashMap.html) | Map | _N_ | _N_ | Y | _N (key)_ | _N_
@@ -1052,7 +1058,7 @@ for (int i = 0; i < nums.size(); i++) {
System.out.println(nums.get(i));
}
nums.remove(nums.size() - 1);
nums.remove(0); // 非常慢
nums.remove(0); // 较慢,因为需要移动后续元素
for (Integer value : nums) {
System.out.println(value);
}
@@ -1205,11 +1211,11 @@ Java I/O流
### 字节流
```java
// 文件输入流
// 文件输入流 (注意:需要手动关闭或使用 try-with-resources
InputStream inputStream
= new FileInputStream("input.txt");
// 文件输出流
// 文件输出流 (注意:需要手动关闭或使用 try-with-resources
OutputStream outputStream
= new FileOutputStream("output.txt");
@@ -1494,8 +1500,8 @@ Class<?>[] interfaces = clazz.getInterfaces();
<!--rehype:wrap-class=col-span-2-->
```java
// 使用默认构造函数创建对象
MyClass instance = (MyClass) clazz.newInstance();
// 使用默认构造函数创建对象注意newInstance() 已废弃)
MyClass instance = (MyClass) clazz.getDeclaredConstructor().newInstance();
// 使用带参数的构造函数创建对象
Constructor<?> constructor = clazz.getConstructor(String.class, int.class);
@@ -1595,6 +1601,10 @@ Consumer<String> test = System.out::println;
Comparator<Integer> comparator = Math::max;
int result = comparator.compare(1, 2);
// 返回 -1
BinaryOperator<Integer> maxOperator = Math::max;
int result = maxOperator.apply(1, 2);
// 返回 2
```
@@ -1602,8 +1612,8 @@ int result = comparator.compare(1, 2);
```java
String str = "HELLO";
String lowerCase = str::toLowerCase;
Supplier<String> lowerCaseSupplier = str::toLowerCase;
String lowerCase = lowerCaseSupplier.get();
// 返回 "hello"
```
@@ -1623,7 +1633,7 @@ Function<Integer, String[]> function = String[]::new;
String[] array = function.apply(5);
// 返回 5 空字符串数组
// 返回长度为 5 空字符串数组
```
<!--rehype:className=wrap-text-->
@@ -1632,7 +1642,8 @@ String[] array = function.apply(5);
```java
String someStr = "HELLO";
String lowerCase = someStr::toLowerCase;
Supplier<String> lowerCaseSupplier = someStr::toLowerCase;
String lowerCase = lowerCaseSupplier.get();
// 返回 "hello"
```
@@ -1641,7 +1652,8 @@ String lowerCase = someStr::toLowerCase;
```java
SomeClass someObject = new SomeClass();
int result = someObject::staticMethod;
Supplier<Integer> methodSupplier = someObject::staticMethod;
int result = methodSupplier.get();
// 调用静态方法
```
@@ -1751,7 +1763,7 @@ text.split(Pattern.quote("|"));
:-|:-
`Math.max(a,b)` | `a``b` 的最大值
`Math.min(a,b)` | `a``b` 的最小值
`Math.abs(a)` | 绝对值
`Math.abs(a)` | `a`绝对值
`Math.sqrt(a)` | `a` 的平方根
`Math.pow(a,b)` | `b` 的幂
`Math.round(a)` | 最接近的整数
@@ -1760,8 +1772,8 @@ text.split(Pattern.quote("|"));
`Math.tan(ang)` | `ang` 的切线
`Math.asin(ang)` | `ang` 的反正弦
`Math.log(a)` | `a` 的自然对数
`Math.toDegrees(rad)` | 以度为单位的角度弧度
`Math.toRadians(deg)` | 以弧度为单位的角度度
`Math.toDegrees(rad)` | 弧度转角
`Math.toRadians(deg)` | 角度转弧
### 异常 Try/Catch/Finally
@@ -1769,9 +1781,12 @@ text.split(Pattern.quote("|"));
try {
// something
} catch (Exception e) {
e.printStackTrace();
// 建议使用日志框架记录异常
logger.error("发生异常", e);
// 或者至少使用标准错误流
// e.printStackTrace();
} finally {
System.out.println("always printed");
System.out.println("总是执行");
}
```
@@ -1779,7 +1794,7 @@ try {
<!--rehype:wrap-class=row-span-2-->
- `ArrayDeque`: 可调整大小的数组双端队列实现了Deque接口
- `Arrays`: 提供静态工厂,允许将数组视为列表
- `Arrays`: 提供操作数组的静态方法,如排序、搜索、比较等
- `Collections`: 包含操作集合或返回集合的静态方法
- `Date`: 表示特定时间瞬间,精度为毫秒
- `Dictionary`: 抽象父类可用于键值对映射例如Hashtable