
以下是一篇关于Java期末考试的技术方案和应用实例文章,涵盖了常见考点及五大类代码,帮助你快速复习应对考试。
Java期末考试通常会考查基础语法、面向对象编程、异常处理、文件操作、数据库连接等多个方面的知识。以下是对这些考点的详细梳理及相关代码示例。
// 变量声明与赋值
int num = 10;
double price = 9.99;
boolean isTrue = true;
// if - else条件判断
if (num > 5) {
System.out.println("数字大于5");
} else {
System.out.println("数字小于等于5");
}
// for循环
for (int i = 0; i < 5; i++) {
System.out.println("循环次数:" + i);
}
// 数组声明与初始化
int[] arr = new int[3];
arr[0] = 1;
arr[1] = 2;// 定义类与创建对象
class Person {
String name;
int age;
public void introduce() {
System.out.println("我叫" + name + ",年龄" + age + "岁。");
}
}
Person p = new Person();
p.name = "张三";
p.age = 20;
p.introduce();
// 继承
class Student extends Person {
String school;
public void study() {
System.out.println("在" + school + "学习。");
}
}
Student s = new Student();
s.name = "李四";
s.age = 18;
s.school = "XX中学";
s.introduce();
s.study();try {
int result = 10 / 0; // 会抛出ArithmeticException异常
} catch (ArithmeticException e) {
System.out.println("发生异常:" + e.getMessage());
} finally {
System.out.println("无论是否异常,都会执行这里");
}import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class FileReadExample {
public static void main(String[] args) {
try (BufferedReader reader = new BufferedReader(new FileReader("test.txt"))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}上述代码使用BufferedReader读取文本文件“test.txt”中的内容,逐行输出。若文件不存在或读取过程中出现其他I/O异常,会在catch块中打印异常堆栈信息。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class JDBCExample {
public static void main(String[] args) {
try (Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/mydb", "root", "password");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM users")) {
while (rs.next()) {
System.out.println("用户ID:" + rs.getInt("id") + ",用户名:" + rs.getString("username"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}该代码通过JDBC连接到本地的MySQL数据库,查询“users”表中的数据,并将结果输出。需确保已添加相应的数据库驱动jar包到项目中。
Java 期末考,Java 考试救急,Java 考点汇总,Java 五大类代码,Java 考试重点,Java 期末复习,Java 考试资料,Java 高频考点,Java 代码示例,Java 考试技巧,Java 期末冲刺,Java 核心考点,Java 考试攻略,Java 编程考点,Java 期末备考
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。