Java 快速入门

Enum

Enum 枚举 可以通过构造函数添加描述信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
enum Code {
INVALID("this is invalid"),
WHATEVER("whatever"),
GOOD("Good");

String msg;

Code(String msg) {
this.msg = msg;
}
}

public class Main {
static void main(String[] args) {
Code code = Code.INVALID;
System.out.println(code.msg);
}
}

HashMap

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Main {
static void main(String[] args) {
HashMap<String, String> map = new HashMap<String,String>();
map.put("A", "something A");
map.put("B", "something B");
map.remove("B");

System.out.println(map); // {A=something A}
System.out.println(map.get("A")); // something A
System.out.println(map.size()); // 1
System.out.println(map.containsKey("A")); // true

map.replace("A", "new A");

System.out.println(map); // {A=new A}
}
}

向上转型与向下转型

向上转型:父类引用指向子类对象
向下转型:子类引用指向父类对象

1
2
3
4
5
6
7
8
9
10
11
public class Main {
static void main(String[] args) {
Parent parent = new Child();
System.out.println(parent.getClass()); // class Child
System.out.println(parent instanceof Child); // true
System.out.println(parent instanceof Parent); // true
}
}

public class Parent {}
public class Child extends Parent {}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

class Animal {
public void display() {
System.out.println("Animal");
}
}

class Dog extends Animal {
public void display() {
System.out.println("dog");
}

public void eat() {
System.out.println("吃骨头");
}
}

public class Main{
public static void main(String[] args) {
//向上转型
Animal animal = new Dog();
animal.display();

//向下转型
//Animal类中原本没有 eat方法,在向下转型之前如果调用eat方法会报错
//向下转型为子类Dog类后,就可以调用子类中特有的方法,而不会报错
animal = (Dog)animal;
((Dog) animal).eat();
}
}

安全性:使用 instanceof 判断

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

public class Main {
public static void main(String[] args) {
//向上转型
Animal animal = new Dog();

//判断instanceof 是否为 true
if(animal instanceof Dog) {
//向下转型
animal = (Dog)animal;
((Dog) animal).eat();
} else {
System.out.println("Animal无法向下转型为Dog");
}
}
}

Lambda

Lambda 表达式

1
2
3
4
5
6
7
8
public class Main {
static void main(String[] args) {
Runnable r = () -> {
System.out.println("Hello World");
}
r.run();
}
}

FunctionalInterface 函数式接口是 Java 8 引入的一种新特性,它是一个有且仅有一个抽象方法的接口,但可以有多个非抽象方法。函数式接口可以被隐式转换为 lambda 表达式,使得代码更加简洁和易读。

1
2
3
4
5
6
7
8
9
10
11
@FunctionalInterface
interface MyInterface {
void DoSome();
}

public class Main{
public static void main(String[] args) {
MyInterface face = () -> System.out.println("das");
face.DoSome();
}
}

泛型 public static <T> void

1
2
3
4
5
6
7
8
9
10
11
public class Main {
public static void main(String[] args) {
displayThing("DSDD");
displayThing(1234);
}


public static <T> void displayThing(T t) {
System.out.println(t);
}
}

泛型类 class MyGenericClass<T>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class Main {
public static void main(String[] args) {
MyGenericClass<Integer> class1 = new MyGenericClass<>(123);
class1.display();

}

}

class MyGenericClass<T> {
T t;

MyGenericClass(T t) {
this.t = t;
}

void display() {
System.out.println(t);
}
}

带有约束的泛型 <T extends Number>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class Main {
public static void main(String[] args) {
displayThing(123);
}
public static <T extends Number> void displayThing(T t) {
System.out.println(t);
}
}

## Thread

```java
public class Main {
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start();
}
}

class MyThread extends Thread {
@Override
public void run() {
System.out.println("Thread is running");
}
}

Thread 和 Runnable 的区别:

  • Runnable 是接口:代表要执行的任务,里面只有一个 run() 方法,描述线程要做什么。
  • Thread 是类:代表线程本身,负责创建、启动、管理线程。Thread 类本身也实现了 Runnable。
1
2
3
4
5
6
7
8
class MyTask implements Runnable {
@Override
public void run() {
System.out.println("线程任务");
}
}
// 把任务交给 Thread 去执行
new Thread(new MyTask()).start();

优点:只是实现接口,不占用继承位;同一个任务对象可以交给多个线程执行。

共享变量例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Task implements Runnable{
private int num = 10;
@Override
public void run() {
while(num>0){
System.out.println(num--);
}
}
}
public class Test{
public static void main(String[] args) {
Task task = new Task();
// 同一个任务,交给2个线程,共享num
new Thread(task).start();
new Thread(task).start();
}
}

ThreadPool

线程池:线程复用,避免频繁创建线程,提高性能。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.util.concurrent.*;

public class ThreadPoolDemo {
public static void main(String[] args) {
// 1. 创建线程池
ThreadPoolExecutor pool = new ThreadPoolExecutor(
2, // 核心线程2个
5, // 最大线程5个
3L, // 非核心线程空闲3秒回收
TimeUnit.SECONDS,
new ArrayBlockingQueue<>(10), // 有界队列,容量10
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.CallerRunsPolicy()
);

// 2. 提交任务:2种方式
// 方式1:execute() 提交Runnable,无返回值
for (int i = 0; i < 15; i++) {
int idx = i;
pool.execute(() -> {
System.out.println(Thread.currentThread().getName() + " 执行任务:" + idx);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}

// 方式2:submit() 提交任务,有返回值Future,可以捕获异常
// Future<?> future = pool.submit(() -> System.out.println("有返回任务"));

// 3. 关闭线程池
pool.shutdown(); // 平缓关闭:不再接收新任务,等待已有任务执行完毕
// pool.shutdownNow(); // 强制关闭:尝试中断正在执行任务,返回未执行任务列表
}
}

Java 快速入门
https://taylorandtony.github.io/2026/10/08/Java-快速入门/
作者
TaylorAndTony
发布于
2026年10月8日
许可协议