Java期末速成
时间: 2025-07-04 14:15:30 浏览: 9
### 重点知识点梳理
#### 1. Java基础语法
Java的基础语法是掌握编程的核心,包括变量、数据类型、运算符、控制结构(如if-else、for循环、while循环)、数组等。理解这些概念对于编写简单到复杂的程序至关重要。
- **变量与数据类型**:Java是一种静态类型语言,所有变量必须先声明再使用。常见的数据类型包括int、double、boolean、char等[^1]。
- **运算符**:Java支持多种运算符,包括算术运算符(+、-、*、/、%)、比较运算符(==、!=、>、<)和逻辑运算符(&&、||、!)。
- **控制结构**:if-else语句用于条件判断,for和while循环用于重复执行代码块。
#### 2. 面向对象编程(OOP)
面向对象编程是Java的核心特性之一,主要包括类与对象的概念、封装、继承、多态等原则。
- **类与对象**:类是一个模板,描述一类对象的行为和状态;对象是类的具体实例。例如,定义一个`Person`类,并创建其对象。
```java
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// Getters and setters
}
```
- **封装**:通过访问修饰符private、protected、public来限制对类成员的直接访问。
- **继承**:允许一个类继承另一个类的属性和方法,从而实现代码重用。例如,`Student`类可以继承`Person`类。
```java
public class Student extends Person {
private String studentId;
public Student(String name, int age, String studentId) {
super(name, age);
this.studentId = studentId;
}
}
```
- **多态**:多态是指同一个接口可以有不同的实现方式。它可以通过方法重载(overloading)和方法重写(overriding)来实现[^1]。
#### 3. 异常处理
异常处理机制可以帮助程序更好地应对运行时错误,保证程序的健壮性和稳定性。主要涉及try-catch-finally语句块以及自定义异常类的设计与应用。
- **try-catch-finally**:try块中放置可能抛出异常的代码,catch块用来捕获并处理异常,finally块无论是否发生异常都会被执行。
```java
try {
FileInputStream fis = new FileInputStream("file.txt");
} catch (FileNotFoundException e) {
System.out.println("File not found.");
} finally {
// Close resources here
}
```
#### 4. 文件操作
Java提供了丰富的API来进行文件读写操作,常用的有FileInputStream/FileOutputStream、BufferedReader/BufferedWriter等流式处理工具。此外,NIO包下的Files类也简化了许多常见任务[^1]。
- **读取文件**:
```java
import java.nio.file.*;
import java.io.*;
public class ReadFileExample {
public static void main(String[] args) throws IOException {
Path path = Paths.get("example.txt");
String content = Files.readString(path);
System.out.println(content);
}
}
```
#### 5. 数据库连接
JDBC(Java Database Connectivity)API使得Java应用程序能够与各种关系型数据库交互。需要了解如何加载驱动程序、建立连接、执行SQL查询及更新操作,并正确关闭所有资源。
- **连接数据库**:
```java
import java.sql.*;
public class JdbcExample {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
// Register JDBC driver
Class.forName("com.mysql.cj.jdbc.Driver");
// Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection("jdbc:mysql://localhost/testdb","username","password");
// Execute a query
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql;
sql = "SELECT id, first, last FROM Employees";
ResultSet rs = stmt.executeQuery(sql);
// Extract data from result set
while(rs.next()){
int id = rs.getInt("id");
String first = rs.getString("first");
String last = rs.getString("last");
// Display values
System.out.print("ID: " + id);
System.out.print(", First: " + first);
System.out.println(", Last: " + last);
}
// Clean up environment
rs.close();
stmt.close();
conn.close();
} catch(SQLException se){
// Handle errors for JDBC
se.printStackTrace();
} catch(Exception e){
// Handle errors for Class.forName
e.printStackTrace();
} finally{
// Finally block used to close resources
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}// nothing we can do
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
}
```
#### 6. 网络通信
利用Socket编程可以在客户端和服务端之间进行网络通信。Java中的ServerSocket和Socket类提供了基本的支持[^1]。
- **服务器端示例**:
```java
import java.net.*;
import java.io.*;
public class EchoServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(8080);
System.out.println("Server is listening on port 8080...");
while (true) {
Socket socket = serverSocket.accept();
System.out.println("New client connected");
InputStream input = socket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
OutputStream output = socket.getOutputStream();
PrintWriter writer = new PrintWriter(output, true);
String text;
while ((text = reader.readLine()) != null) {
System.out.println("Received: " + text);
writer.println(text);
}
socket.close();
}
}
}
```
阅读全文
相关推荐

















