java期末复习基础知识
时间: 2025-07-01 08:59:01 浏览: 5
### Java基础知识复习:期末考试重点
Java 是一门面向对象的编程语言,广泛用于企业级应用开发、Web 应用、Android 移动端等场景。为了应对期末考试,以下内容将系统性地梳理 Java 编程语言的基础知识与考试重点。
---
#### 一、面向对象编程(OOP)
1. **类与对象**
类是对象的模板,对象是类的具体实例。在 Java 中,通过 `class` 关键字定义类,并使用 `new` 创建对象。
```java
class Person {
String name;
int age;
void sayHello() {
System.out.println("Hello, my name is " + name);
}
}
public class Main {
public static void main(String[] args) {
Person p = new Person();
p.name = "Alice";
p.sayHello(); // 输出: Hello, my name is Alice
}
}
```
2. **访问修饰符**
Java 提供了三种主要的访问控制修饰符:
- `private`:仅在本类中可访问。
- `protected`:在本包和子类中可访问。
- `public`:所有地方均可访问[^1]。
3. **继承与多态**
继承通过 `extends` 实现,子类可以继承父类的非私有成员。多态允许子类重写父类方法,实现运行时绑定。
```java
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Animal a = new Dog();
a.sound(); // 输出: Dog barks
}
}
```
---
#### 二、输入输出流(I/O)
Java 提供了丰富的 I/O 操作类,如 `FileInputStream`、`FileOutputStream`、`BufferedReader` 和 `PrintWriter` 等,用于处理文件读写操作。
```java
import java.io.*;
public class FileIOExample {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new FileReader("input.txt"))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
---
#### 三、异常处理
Java 使用 `try-catch-finally` 结构来捕获和处理异常,也可以使用 `throw` 抛出异常。
```java
public class ExceptionHandling {
public static void main(String[] args) {
try {
int result = divide(10, 0);
System.out.println(result);
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero!");
}
}
public static int divide(int a, int b) {
if (b == 0) {
throw new ArithmeticException("Division by zero");
}
return a / b;
}
}
```
---
#### 四、图形用户界面(GUI)
Java 提供了 AWT 和 Swing 两个 GUI 开发库。Swing 更加灵活且功能丰富,适合现代桌面应用开发。
```java
import javax.swing.*;
public class SimpleGUI {
public static void main(String[] args) {
JFrame frame = new JFrame("Simple GUI");
JButton button = new JButton("Click Me!");
button.addActionListener(e -> JOptionPane.showMessageDialog(frame, "Button clicked!"));
frame.add(button);
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
```
---
#### 五、多线程
Java 支持多线程编程,可以通过继承 `Thread` 类或实现 `Runnable` 接口创建线程。
```java
class MyThread implements Runnable {
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + " - " + i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class MultiThreadingExample {
public static void main(String[] args) {
Thread t1 = new Thread(new MyThread(), "Thread-1");
Thread t2 = new Thread(new MyThread(), "Thread-2");
t1.start();
t2.start();
}
}
```
---
#### 六、泛型与集合框架
Java 泛型提供编译时类型安全检查,避免运行时 ClassCastException。常见的集合类包括 `ArrayList`、`HashMap`、`HashSet` 等。
```java
import java.util.ArrayList;
public class GenericExample {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
for (String fruit : list) {
System.out.println(fruit);
}
}
}
```
---
#### 七、Java 文件结构与跨平台原理
Java 的“一次编写,到处运行”特性依赖于 JVM(Java 虚拟机)。Java 源代码(`.java`)被编译为字节码(`.class`),然后由 JVM 解释执行,从而实现跨平台兼容性[^1]。
---
#### 八、临界资源与同步机制
多个线程访问共享资源时,需要使用同步机制保证数据一致性。Java 提供了 `synchronized` 关键字和 `ReentrantLock` 类来实现线程同步。
```java
class Counter {
private int count = 0;
public synchronized void increment() {
count++;
}
public int getCount() {
return count;
}
}
public class SyncExample {
public static void main(String[] args) throws InterruptedException {
Counter counter = new Counter();
Thread t1 = new Thread(() -> {
for (int i = 0; i < 1000; i++) {
counter.increment();
}
});
Thread t2 = new Thread(() -> {
for (int i = 0; i < 1000; i++) {
counter.increment();
}
});
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println("Final count: " + counter.getCount()); // 应为 2000
}
}
```
---
#### 九、泛型方法示例
泛型方法允许在调用时指定类型参数,增强灵活性。
```java
public class GenericMethods {
public static <T extends Comparable<T>> int compare(T a, T b) {
return a.compareTo(b);
}
public static void main(String[] args) {
System.out.println(compare(1.1, 2.5)); // 输出负数
System.out.println(compare(9.9, 9.9)); // 输出 0
System.out.println(compare(7.8, 3.9)); // 输出正数
}
}
```
---
阅读全文
相关推荐














