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