Java 核心内容学习笔记
一、Java 环境配置
1. JDK 安装
- 下载地址:Oracle 官方网站
- 安装步骤:
- 根据操作系统选择对应的安装包
- 运行安装程序,选择安装路径
- 配置环境变量
- JAVA_HOME:指向JDK安装目录
- PATH:添加 %JAVA_HOME%\bin
- CLASSPATH:添加 .;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar
2. 验证安装
- 打开命令提示符,输入
java -version
,若显示Java版本信息,则安装成功
二、Java 基础语法
1. 第一个Java程序
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
- 文件名必须与类名相同
main
方法是程序入口
2. 变量与数据类型
// 基本数据类型
byte a = 10;
short b = 20;
int c = 100;
long d = 1000L;
float e = 3.14f;
double f = 3.1415926;
char g = 'A';
boolean h = true;
// 字符串
String str = "Hello, Java!";
3. 运算符
// 算术运算符
int sum = 10 + 20;
int diff = 20 - 10;
int product = 10 * 20;
int quotient = 20 / 10;
int remainder = 20 % 3;
// 关系运算符
boolean isEqual = (10 == 20);
boolean isNotEqual = (10 != 20);
boolean isGreater = (10 > 20);
boolean isLess = (10 < 20);
// 逻辑运算符
boolean andResult = (true && false);
boolean orResult = (true || false);
boolean notResult = !true;
4. 控制流程
// if-else语句
if (condition) {
// 执行代码
} else if (anotherCondition) {
// 执行代码
} else {
// 执行代码
}
// switch语句
switch (expression) {
case value1:
// 执行代码
break;
case value2:
// 执行代码
break;
default:
// 执行代码
}
// for循环
for (int i = 0; i < 10; i++) {
System.out.println(i);
}
// while循环
int i = 0;
while (i < 10) {
System.out.println(i);
i++;
}
// do-while循环
int i = 0;
do {
System.out.println(i);
i++;
} while (i < 10);
三、面向对象编程
1. 类与对象
// 定义类
public class Car {
// 属性
String color;
int speed;
// 构造方法
public Car(String color, int speed) {
this.color = color;
this.speed = speed;
}
// 方法
public void accelerate(int increment) {
speed += increment;
}
public void displayInfo() {
System.out.println("Color: " + color + ", Speed: " + speed);
}
}
// 创建对象
Car myCar = new Car("Red", 100);
myCar.accelerate(20);
myCar.displayInfo();
2. 继承
// 父类
public class Animal {
public void eat() {
System.out.println("Eating...");
}
}
// 子类
public class Dog extends Animal {
public void bark() {
System.out.println("Barking...");
}
}
// 使用
Dog dog = new Dog();
dog.eat(); // 继承自父类的方法
dog.bark(); // 子类自己的方法
3. 多态
// 父类引用指向子类对象
Animal animal = new Dog();
animal.eat(); // 调用的是Dog类的eat方法(如果重写了父类方法)
// 方法重写
public class Dog extends Animal {
@Override
public void eat() {
System.out.println("Dog eating...");
}
}
4. 接口
// 定义接口
public interface Movable {
void move();
}
// 实现接口
public class Car implements Movable {
@Override
public void move() {
System.out.println("Car is moving...");
}
}
// 使用
Car car = new Car();
car.move();
四、异常处理
1. 异常类型
Exception
:常见异常的父类RuntimeException
:运行时异常,如空指针异常、数组越界等IOException
:输入输出异常ClassNotFoundException
:类未找到异常
2. 异常处理机制
try {
// 可能抛出异常的代码
int result = 10 / 0;
} catch (ArithmeticException e) {
// 捕获并处理异常
System.out.println("ArithmeticException caught: " + e.getMessage());
} catch (Exception e) {
// 捕获其他异常
System.out.println("Exception caught: " + e.getMessage());
} finally {
// 无论是否发生异常都会执行的代码
System.out.println("Finally block executed");
}
3. 自定义异常
// 自定义异常类
public class MyException extends Exception {
public MyException(String message) {
super(message);
}
}
// 抛出自定义异常
public class Test {
public static void main(String[] args) {
try {
throw new MyException("This is a custom exception");
} catch (MyException e) {
System.out.println(e.getMessage());
}
}
}
五、集合框架
1. List 接口
import java.util.ArrayList;
import java.util.List;
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Orange");
for (String fruit : list) {
System.out.println(fruit);
}
2. Set 接口
import java.util.HashSet;
import java.util.Set;
Set<String> set = new HashSet<>();
set.add("Apple");
set.add("Banana");
set.add("Orange");
for (String fruit : set) {
System.out.println(fruit);
}
3. Map 接口
import java.util.HashMap;
import java.util.Map;
Map<String, Integer> map = new HashMap<>();
map.put("Apple", 10);
map.put("Banana", 20);
map.put("Orange", 30);
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
六、输入输出流
1. 文件读取
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class ReadFileExample {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new FileReader("example.txt"))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
2. 文件写入
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class WriteFileExample {
public static void main(String[] args) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter("example.txt", true))) {
bw.write("Hello, Java!");
bw.newLine();
} catch (IOException e) {
e.printStackTrace();
}
}
}
七、多线程
1. 创建线程
// 继承Thread类
class MyThread extends Thread {
@Override
public void run() {
System.out.println("Thread is running...");
}
}
// 实现Runnable接口
class MyRunnable implements Runnable {
@Override
public void run() {
System.out.println("Runnable is running...");
}
}
// 使用
MyThread thread = new MyThread();
thread.start();
Thread thread2 = new Thread(new MyRunnable());
thread2.start();
2. 线程同步
public class SynchronizedExample {
private int count = 0;
public synchronized void increment() {
count++;
}
public synchronized int getCount() {
return count;
}
}
八、常用类库
1. String 类
String str1 = "Hello";
String str2 = "World";
String str3 = str1 + " " + str2;
int length = str3.length();
char charAt = str3.charAt(0);
boolean equals = str3.equals("Hello World");
boolean startsWith = str3.startsWith("Hello");
boolean endsWith = str3.endsWith("World");
2. 数组操作
int[] array = {1, 2, 3, 4, 5};
// 遍历
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
// 拷贝
int[] copiedArray = new int[array.length];
System.arraycopy(array, 0, copiedArray, 0, array.length);
// 转换为字符串
String arrayStr = Arrays.toString(array);
3. 日期与时间
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDate = now.format(formatter);
System.out.println(formattedDate);
以上是Java核心内容的学习笔记,希望对刚入门的新手程序员有所帮助!如果在学习过程中有任何疑问,欢迎随时提问。
📚 推荐阅读
- wx群聊总结助手:一个基于人工智能的群聊消息总结工具,支持多种AI服务,可以自动提取群聊重点内容并生成结构化总结
- 历时两周半开发的一款加载live2模型的浏览器插件
- PySide6+live2d+小智 开发的 AI 语音助手桌面精灵,支持和小智持续对话、音乐播放、番茄时钟、书签跳转、ocr等功能
- github优秀开源作品集