基于Java的相关知识点

一、Java基础语法

  1. 基础语法结构

// 数据类型

int, double, boolean, char

String, 数组

// 控制结构

if-else, switch-case

for, while, do-while

// 运算符

算术、关系、逻辑、位运算符

三元运算符 ? :

  1. 面向对象编程(OOP)

// 四大特性

class Person {

// 封装

private String name;

// 构造方法

public Person(String name) {

this.name = name;

}

// 多态

public void introduce() {

System.out.println("I am a person");

}

}

// 继承

class Student extends Person {

@Override

public void introduce() {

System.out.println("I am a student");

}

}

// 抽象类与接口

abstract class Animal {

abstract void makeSound();

}

interface Runnable {

void run();

}

二、核心API

  1. 集合框架

// List接口

ArrayList<String> list = new ArrayList<>();

LinkedList<Integer> linkedList = new LinkedList<>();

// Set接口

HashSet<String> hashSet = new HashSet<>();

TreeSet<Integer> treeSet = new TreeSet<>();

// Map接口

HashMap<String, Integer> hashMap = new HashMap<>();

TreeMap<String, String> treeMap = new TreeMap<>();

// 迭代器

Iterator<String> it = list.iterator();

  1. 异常处理

try {

// 可能抛出异常的代码

int result = 10 / 0;

} catch (ArithmeticException e) {

System.out.println("除零错误");

} catch (Exception e) {

System.out.println("通用异常");

} finally {

// 清理资源

}

三、高级特性

  1. 并发编程

// 创建线程

Thread thread = new Thread(() -> {

System.out.println("线程执行");

});

// 线程池

ExecutorService executor = Executors.newFixedThreadPool(5);

// 同步机制

synchronized void syncMethod() {}

ReentrantLock lock = new ReentrantLock();

// 并发集合

ConcurrentHashMap<String, String> concurrentMap = new ConcurrentHashMap<>();

CopyOnWriteArrayList<String> copyOnWriteList = new CopyOnWriteArrayList<>();

  1. IO与NIO

// 传统IO

FileInputStream fis = new FileInputStream("file.txt");

BufferedReader br = new BufferedReader(new FileReader("file.txt"));

// NIO

Path path = Paths.get("file.txt");

Files.readAllLines(path);

// 通道和缓冲区

ByteBuffer buffer = ByteBuffer.allocate(1024);

FileChannel channel = FileChannel.open(path);

  1. 反射机制

Class<?> clazz = Class.forName("com.example.Person");

Object obj = clazz.newInstance();

// 获取方法并调用

Method method = clazz.getMethod("setName", String.class);

method.invoke(obj, "张三");

// 获取字段

Field field = clazz.getDeclaredField("name");

field.setAccessible(true);

四、JVM与性能优化

  1. 内存模型

· 堆内存(Heap):对象实例

· 栈内存(Stack):局部变量、方法调用

· 方法区(Method Area):类信息、常量池

· 程序计数器

· 本地方法栈

  1. 垃圾回收

// GC算法

  • 标记-清除

  • 复制算法

  • 标记-整理

  • 分代收集

// GC调优参数

-Xms512m -Xmx1024m -XX:+UseG1GC

  1. 类加载机制

· 加载 → 验证 → 准备 → 解析 → 初始化

· 双亲委派模型

· 自定义类加载器

五、现代Java特性(Java 8+)

  1. Lambda表达式

List<String> names = Arrays.asList("Tom", "Jerry");

names.forEach(name -> System.out.println(name));

names.forEach(System.out::println);

  1. Stream API

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

int sum = numbers.stream()

.filter(n -> n % 2 == 0)

.map(n -> n * 2)

.reduce(0, Integer::sum);

  1. 新日期时间API

LocalDate today = LocalDate.now();

LocalDateTime now = LocalDateTime.now();

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

  1. Optional类

Optional<String> optional = Optional.ofNullable(getName());

optional.ifPresent(name -> System.out.println(name));

String result = optional.orElse("default");

六、框架与工具

  1. Spring框架核心

· IoC(控制反转)

· AOP(面向切面编程)

· Spring MVC

· Spring Boot自动配置

  1. 数据库操作

// JDBC

Connection conn = DriverManager.getConnection(url, user, password);

PreparedStatement ps = conn.prepareStatement("SELECT * FROM users");

// JPA/Hibernate

@Entity

public class User {

@Id

@GeneratedValue(strategy = GenerationType.IDENTITY)

private Long id;

private String username;

}

  1. 构建工具

· Maven依赖管理

· Gradle构建脚本

· JUnit单元测试

七、设计模式(常用)

  1. 创建型模式

// 单例模式

public class Singleton {

private static volatile Singleton instance;

private Singleton() {}

public static Singleton getInstance() {

if (instance == null) {

synchronized (Singleton.class) {

if (instance == null) {

instance = new Singleton();

}

}

}

return instance;

}

}

// 工厂模式

// 建造者模式

  1. 结构型模式

· 适配器模式

· 代理模式

· 装饰器模式

  1. 行为型模式

· 观察者模式

· 策略模式

· 模板方法模式

八、最佳实践

  1. 代码规范

· 命名规范(驼峰命名法)

· 代码注释规范

· 异常处理原则

  1. 性能优化

// 字符串拼接使用StringBuilder

StringBuilder sb = new StringBuilder();

for (int i = 0; i < 100; i++) {

sb.append(i);

}

// 集合初始化指定容量

ArrayList<String> list = new ArrayList<>(100);

HashMap<String, String> map = new HashMap<>(128);

  1. 安全性

· SQL注入防护

· XSS攻击防范

· 密码加密存储

学习路线建议

  1. 初级阶段(1-3个月):

· Java基础语法

· 面向对象编程

· 集合框架

· 异常处理

  1. 中级阶段(3-6个月):

· 多线程编程

· IO/NIO

· JVM基础

· 数据库操作

  1. 高级阶段(6-12个月):

· 设计模式

· 框架原理(Spring)

· 分布式系统

· 性能调优

  1. 专家阶段(1年以上):

· JVM深度优化

· 高并发系统设计

· 微服务架构

· 源码分析

相关推荐
lisw052 小时前
稀疏算法概述!
算法·机器人·无人机
qq_452396232 小时前
第三篇:Transactional Outbox 模式详解
java·kubernetes·rocketmq
IT空门:门主3 小时前
Spring AI 2.1.0 更新了什么:从 2.0 升上来的完整梳理
人工智能·python·spring
3095566663 小时前
股票接口重试怎么分类:正常、空数组与错误响应的幂等处理
java·大数据·开发语言·数据库·php·api接口·股票数据api接口
AI创界者3 小时前
Python 进阶实战(七十章):遗留系统重构、渐进式绞杀与技术债治理实战
人工智能·python·aigc
YLCHUP4 小时前
【括号匹配+dp优化】题解:双端括号(brackets)_动态规划_状态优化_数学建模_贪心_性质挖掘_STLmap_C++算法竞赛
数据结构·c++·笔记·算法·数学建模·贪心算法·动态规划
ZhengEnCi4 小时前
电脑操作 Agent 一直点不中:截图没更新时,提示词该怎么改才能停止瞎试
人工智能·python·agent
凌镜icecraft4 小时前
我改文件扩展名的时候那个警告弹窗再也不会弹出来了
windows·c#·wpf·explorer
郝学胜-神的一滴5 小时前
Effective STL 条款09:慎重选择删除元素的方法
开发语言·网络·数据结构·c++·算法·stl