Java 基础知识
一、Java 核心概念
- 跨平台原理
Java 程序通过 JVM(Java 虚拟机)实现跨平台特性,编译后的.class
字节码文件可在任何安装 JVM 的设备运行。
// 编译命令:javac Main.java → 生成 Main.class
// 执行命令:java Main
public class Main {
public static void main(String[] args) {
System.out.println("Hello JVM!");
}
}
- JDK 与 JRE 的区别
- JDK:开发工具包(含 JRE + 编译器 javac)
- JRE:运行环境(含 JVM + 核心类库)
二、开发环境配置
Windows 环境变量配置示例(PATH 添加 JDK/bin)
setx PATH "%PATH%;C:\Program Files\Java\jdk-21\bin"
!
三、基础语法精讲
- 数据类型与变量
// 基本数据类型
int age = 25; // 4 字节
double price = 9.99; // 8 字节
char grade = 'A'; // 2 字节
boolean isJavaFun = true; // 1 位
// 引用类型
String message = "Learn Java!"; // 不可变对象
- 流程控制
// 增强 for 循环
int[] numbers = {1,2,3,4,5};
for(int num : numbers) {
System.out.print(num + " "); // 输出:1 2 3 4 5
}
// Switch 表达式(Java 14+)
String day = "MON";
String type = switch(day) {
case "MON","TUE" -> "工作日";
default -> "周末";
};
四、面向对象编程
- 类与对象
// 继承与多态示例
class Animal {
void sound() { System.out.println("动物叫声"); }
}
class Dog extends Animal {
@Override
void sound() { System.out.println("汪汪"); }
}
// 测试类
public class Test {
public static void main(String[] args) {
Animal myDog = new Dog();
myDog.sound(); // 输出:汪汪
}
}
- 三大特性对比
| 特性 | 实现方式 | 代码示例 |
|------------|------------------------------|---------------------------|
| 封装 | private + getter/setter |student.setName("Tom")
|
| 继承 | extends 关键字 |class Sub extends Super
|
| 多态 | 方法重写 + 父类引用指向子类对象 |Animal a = new Dog()
|
五、常用 API 实战
- String 操作
// 字符串不可变性验证
String s1 = "java";
String s2 = s1.concat("!");
System.out.println(s1); // 输出:java(原值未变)
System.out.println(s2); // 输出:java!
// StringBuilder 高效操作
StringBuilder sb = new StringBuilder();
sb.append("Hello").append(" World");
System.out.println(sb.reverse()); // 输出:dlroW olleH
- 时间处理(Java 8+)
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
System.out.println(now.format(formatter)); // 2025-02-20 17:34
// 计算日期间隔
LocalDate start = LocalDate.of(2025,1,1);
long days = ChronoUnit.DAYS.between(start, LocalDate.now());
六、异常处理机制
try {
int[] arr = new int[5];
arr[10] = 50; // 抛出 ArrayIndexOutOfBoundsException
} catch (ArrayIndexOutOfBoundsException e) {
System.err.println("错误:数组越界");
e.printStackTrace();
} finally {
System.out.println("资源清理完成");
}
七、集合框架详解
// ArrayList 与 HashMap 联用
List<Map<String, Object>> students = new ArrayList<>();
Map<String, Object> stu1 = new HashMap<>();
stu1.put("name", "张三");
stu1.put("age", 20);
students.add(stu1);
// 遍历示例
students.forEach(student ->
System.out.println(student.get("name") + ":" + student.get("age"))
);
八、I/O 流操作
// 文件复制(try-with-resources)
try (FileInputStream fis = new FileInputStream("source.txt");
FileOutputStream fos = new FileOutputStream("dest.txt")) {
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) != -1) {
fos.write(buffer, 0, length);
}
} catch (IOException e) {
e.printStackTrace();
}
九、多线程编程
// 实现 Runnable 接口
class MyRunnable implements Runnable {
public void run() {
System.out.println(Thread.currentThread().getName() + "运行中");
}
}
// 创建线程池
ExecutorService pool = Executors.newFixedThreadPool(3);
pool.execute(new MyRunnable());
pool.shutdown();
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
· 凌晨三点救火实录:Java内存泄漏的七个神坑,你至少踩过三个!