前言
这里只是简单列出Java语言的知识点
一、基础语法
1.1 程序结构
1 2 3 4 5 6 7
| public class Main { public static void main(String[] args) { System.out.println("Hello World!"); } }
|
1.2 数据类型
| 类型 |
说明 |
示例 |
byte |
8位整数 |
byte b = 100; |
short |
16位整数 |
short s = 1000; |
int |
32位整数 |
int i = 100000; |
long |
64位整数 |
long l = 100000L; |
float |
32位浮点 |
float f = 3.14f; |
double |
64位浮点 |
double d = 3.14159; |
char |
16位Unicode |
char c = 'A'; |
boolean |
布尔值 |
boolean flag = true; |
String |
字符串(类) |
String str = "Java"; |
1.3 变量与常量
1 2 3
| int count = 10; final double PI = 3.14; var list = new ArrayList<String>();
|
二、运算符
2.1 算术运算符
2.2 关系运算符
2.3 逻辑运算符
2.4 位运算符
2.5 赋值运算符
1
| = += -= *= /= %= &= |= ^= <<= >>= >>>=
|
2.6 三目运算符
1
| condition ? expr1 : expr2
|
2.7 instanceof 运算符
三、流程控制
3.1 条件语句
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| if (condition) { } else if (condition2) { } else { }
switch (variable) { case 1: break; default: }
|
3.2 循环结构
1 2 3 4 5 6 7 8 9 10 11
| for (int i = 0; i < 10; i++) { }
for (String item : collection) { }
while (condition) { }
do { } while (condition);
|
四、方法与函数
4.1 方法定义
1 2 3 4
| [修饰符] 返回类型 方法名(参数列表) { return 返回值; }
|
4.2 方法重载
1 2
| void print(int i) { } void print(String s) { }
|
4.3 可变参数
1
| void method(String... args) { }
|
4.4 Lambda表达式(Java 8+)
1
| Function<Integer, Integer> square = x -> x * x;
|
五、面向对象编程
5.1 类与对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public class Person { private String name; public Person(String name) { this.name = name; } public String getName() { return name; } }
|
5.2 继承
1 2 3
| class Student extends Person { }
|
5.3 多态
1 2 3 4 5 6 7
| interface Animal { void sound(); }
class Dog implements Animal { public void sound() { System.out.println("Woof"); } }
|
5.4 抽象类与接口
1 2 3 4 5 6 7
| abstract class Shape { abstract void draw(); }
interface Drawable { void draw(); }
|
六、异常处理
6.1 try-catch-finally
1 2 3 4 5 6 7
| try { } catch (IOException e) { } finally { }
|
6.2 throws声明
1
| void readFile() throws IOException { }
|
6.3 自定义异常
1 2 3
| class MyException extends Exception { }
|
七、集合框架
7.1 主要接口
1 2 3
| List<String> list = new ArrayList<>(); Set<Integer> set = new HashSet<>(); Map<String, Integer> map = new HashMap<>();
|
7.2 常用操作
1 2 3
| list.add("Java"); set.contains(5); map.put("key", 10);
|
7.3 流式操作(Java 8+)
1 2 3 4
| list.stream() .filter(s -> s.length() > 3) .map(String::toUpperCase) .forEach(System.out::println);
|
八、输入输出
8.1 文件读写
1 2 3 4 5 6
| try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) { String line; while ((line = br.readLine()) != null) { System.out.println(line); } }
|
8.2 标准I/O
1 2
| Scanner sc = new Scanner(System.in); String input = sc.nextLine();
|
九、多线程
9.1 线程创建
1 2 3 4 5 6 7 8
| class MyThread extends Thread { public void run() { } }
Runnable task = () -> { }; new Thread(task).start();
|
9.2 线程池
1 2
| ExecutorService executor = Executors.newFixedThreadPool(5); executor.submit(() -> System.out.println("Task running"));
|
9.3 同步机制
1 2 3
| synchronized (lockObject) { }
|
十、现代Java特性
10.1 记录类(Java 16+)
1
| record Point(int x, int y) { }
|
10.2 模式匹配(Java 17+)
1 2 3
| if (obj instanceof String s) { System.out.println(s.length()); }
|
10.3 密封类(Java 17+)
1
| sealed class Shape permits Circle, Square { }
|
十一、注解与反射
11.1 内置注解
1 2 3
| @Override @Deprecated @SuppressWarnings("unchecked")
|
11.2 自定义注解
1 2 3 4 5
| @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotation { String value() default ""; }
|
11.3 反射API
1 2
| Class<?> clazz = Class.forName("java.lang.String"); Method method = clazz.getMethod("length");
|
十二、模块系统(Java 9+)
12.1 模块声明
1 2 3 4
| module com.example.myapp { requires java.base; exports com.example.mypackage; }
|
十三、最佳实践
- 遵循命名规范(驼峰命名法)
- 使用面向接口编程
- 优先使用集合框架而非数组
- 合理使用异常处理机制
- 避免使用原始类型(优先泛型)
- 使用try-with-resources管理资源
- 遵循SOLID设计原则
- 使用不可变对象提高线程安全性
- 合理使用Optional避免NullPointerException
- 保持代码可读性和可维护性
注:本文档涵盖Java 8到Java 17的主要语法特性,实际使用时需注意JDK版本支持情况。
版权声明: 此文章版权归曦曦所有,如有转载,请注明来自原作者