赞
踩
目录
第九章:Java 8新特性 - Lambda表达式与函数式编程
18.3 Finally块和Try-with-resources语句
第二十章:Java注解处理器(Annotation Processing)
1.1 Java简介与安装配置
Java是由Sun Microsystems公司(后被Oracle收购)在1995年推出的面向对象的编程语言,以其“一次编写,到处运行”(Write Once, Run Anywhere, WORA)的特点而闻名。要开始Java编程之旅,首先需要安装Java Development Kit(JDK),它是Java开发必备的工具包,包含Java编译器、Java虚拟机(JVM)以及其他开发工具。
下载安装Java JDK: 请访问Oracle官网或其他可靠的源下载适合您操作系统的最新版JDK,并按照官方指南进行安装。安装完成后,设置好JAVA_HOME环境变量,确保系统能正确识别Java路径。
1.2 HelloWorld示例
让我们通过经典的“Hello, World!”程序来体验一下Java编程的基础流程。
HelloWorld.java:
- 1public class HelloWorld {
- 2 public static void main(String[] args) {
- 3 System.out.println("Hello, World!"); // 输出文本至控制台
- 4 }
- 5}
编译与运行:
HelloWorld.java
源文件的目录,执行以下命令进行编译:javac HelloWorld.java
HelloWorld.class
字节码文件,接着执行以下命令运行程序:java HelloWorld
此时,你将在控制台看到输出:"Hello, World!"
1.3 Java基础语法概览
标识符:Java中用于命名变量、类、方法等的名称,必须以字母、下划线(_)或美元符号($)开头,后续可跟字母、数字、下划线或美元符号。
注释:单行注释用//
,多行注释用/* */
。
- 1// 这是一个单行注释
- 2/*
- 3 这是一个
- 4 多行注释
- 5 */
- 1int age = 25; // 声明并初始化一个整型变量
- 2String name = "Alice"; // 声明并初始化一个字符串变量
通过这一章的简单介绍,我们对Java有了初步的认识,并动手编写并运行了一个最基础的Java程序。在后续章节中,我们将逐步深入Java的各个知识点,包括数据类型、运算符、控制结构、类和对象、数组、方法、继承、封装和多态等核心概念,助你一步步成为合格的Java开发者。
2.1 Java基本数据类型
Java中有两种类型的变量:基本类型和引用类型。基本类型是Java中最基本的数据形式,它们直接存储值,而非像引用类型那样存储对对象的引用。
基本数据类型列表:
- 1byte aByte = 127;
- 2short aShort = 32767;
- 3int anInt = 1000000;
- 4long aLong = 9223372036854775807L; // L后缀表明这是一个long类型数值
- 1float aFloat = 3.14f; // f后缀表明这是一个float类型数值
- 2double aDouble = 3.14159265358979323846;
1char aChar = 'A'; // 单引号包裹单个字符
1boolean aBoolean = true;
2.2 变量声明与初始化
在Java中,变量必须先声明再使用,并且可以同时声明并初始化。
- 1int myNumber; // 声明一个int类型的变量myNumber,但未初始化
- 2myNumber = 42; // 初始化变量
- 3
- 4// 同时声明并初始化
- 5int anotherNumber = 24;
- 6String myString = "Hello Java!";
2.3 数据类型转换
在必要时,可以进行类型转换,但需要注意可能的精度损失或溢出。
- 1int smallInt = 123;
- 2long bigInt = (long)smallInt; // 自动类型转换,无需显式强制类型转换
- 3
- 4double pi = 3.14159;
- 5int roundedPi = (int)pi; // 强制类型转换,可能会丢失小数部分
通过理解和掌握基本数据类型以及变量的声明和初始化,是学习Java乃至任何编程语言的第一步。在后续章节中,我们将深入探讨运算符、控制结构、数组、类与对象等内容,进一步丰富您的Java编程技能。
3.1 算术运算符
Java提供了一系列算术运算符,包括加(+)、减(-)、乘(*)、除(/)、取模(%)以及自增(++)和自减(--)运算符。
- 1int a = 10, b = 5;
- 2int sum = a + b; // 加法:15
- 3int diff = a - b; // 减法:5
- 4int product = a * b; // 乘法:50
- 5int quotient = a / b; // 除法:2
- 6int remainder = a % b; // 取模(余数):0
- 7
- 8a++; // a自增后变为11
- 9b--; // b自减后变为4
3.2 关系运算符与布尔逻辑运算符
关系运算符用于比较两个值的关系,结果为布尔值(true
或 false
)。
- 1int x = 10, y = 20;
- 2
- 3if (x > y) { // 大于
- 4 System.out.println("x is greater than y");
- 5} else if (x < y) { // 小于
- 6 System.out.println("x is less than y");
- 7} else {
- 8 System.out.println("x is equal to y");
- 9}
- 10
- 11boolean isGreaterOrEqual = x >= y; // 大于等于
- 12boolean isLessOrEqual = x <= y; // 小于等于
- 13boolean isEqual = x == y; // 等于
- 14boolean isNotEqual = x != y; // 不等于
- 15
- 16boolean andResult = (x > 5) && (y > 15); // 逻辑与(AND)
- 17boolean orResult = (x < 15) || (y < 25); // 逻辑或(OR)
- 18boolean notResult = !(x == y); // 逻辑非(NOT)
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
3.3 位运算符
位运算符是对二进制位进行操作,适用于整型和字符型数据。
- 1int bitPattern1 = 0b00010101; // 二进制表示的整数
- 2int bitPattern2 = 0b00101010;
- 3
- 4int bitwiseAnd = bitPattern1 & bitPattern2; // 按位与
- 5int bitwiseOr = bitPattern1 | bitPattern2; // 按位或
- 6int bitwiseXor = bitPattern1 ^ bitPattern2; // 按位异或
- 7int bitwiseNot = ~bitPattern1; // 按位取反
- 8int leftShift = bitPattern1 << 2; // 左移两位
- 9int rightShift = bitPattern1 >> 2; // 右移两位
理解并熟练掌握Java中的各种运算符和表达式是编写复杂程序的基础。在后续章节中,我们将进一步探讨控制结构、数组、类与对象等方面的知识,使您对Java编程有更深的理解和实战能力。
4.1 条件控制
条件控制语句包括if
、if-else
和switch
,用于根据特定条件执行不同的代码块。
if-else语句示例:
- 1int score = 85;
- 2if (score >= 90) {
- 3 System.out.println("优秀");
- 4} else if (score >= 80) {
- 5 System.out.println("良好");
- 6} else {
- 7 System.out.println("一般");
- 8}
switch语句示例:
- 1String dayOfWeek = "Monday";
- 2switch (dayOfWeek) {
- 3 case "Monday":
- 4 System.out.println("新的一周开始了!");
- 5 break;
- 6 case "Friday":
- 7 System.out.println("周末快来了!");
- 8 break;
- 9 case "Saturday":
- 10 case "Sunday":
- 11 System.out.println("今天是周末!");
- 12 break;
- 13 default:
- 14 System.out.println("今天是工作日!");
- 15 break;
- 16}
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
4.2 循环控制
Java提供了for
、while
和do-while
三种循环结构。
for循环示例:
- 1for (int i = 1; i <= 10; i++) {
- 2 System.out.println(i);
- 3}
while循环示例:
- 1int counter = 1;
- 2while (counter <= 10) {
- 3 System.out.println(counter);
- 4 counter++;
- 5}
do-while循环示例:
- 1int attempt = 0;
- 2do {
- 3 System.out.println("尝试第" + (++attempt) + "次登录...");
- 4 // 登录逻辑...
- 5} while (!isLoginSuccessful());
4.3 跳转语句
Java提供了break
、continue
和return
三个跳转语句。
break语句:用于退出当前循环或switch结构。
- 1for (int i = 0; i < 10; i++) {
- 2 if (i == 5) {
- 3 break; // 当i等于5时跳出循环
- 4 }
- 5 System.out.println(i);
- 6}
continue语句:跳过当前迭代的剩余部分,继续下一个迭代。
- 1for (int i = 0; i < 10; i++) {
- 2 if (i % 2 == 0) {
- 3 continue; // 若i为偶数,则跳过本次循环,进入下一次循环
- 4 }
- 5 System.out.println(i);
- 6}
return语句:在函数或方法中,用于结束当前方法的执行,并可选择性地返回一个值。
- 1public int calculateFactorial(int n) {
- 2 if (n == 0) {
- 3 return 1; // 当n为0时,返回1并结束函数
- 4 }
- 5 return n * calculateFactorial(n - 1); // 递归调用自身
- 6}
通过熟练掌握Java的条件控制和循环结构,开发者可以根据程序运行状态灵活控制代码执行流程,实现复杂的逻辑操作。在后续章节中,我们将深入探讨数组、类与对象、继承、多态等相关内容,进一步提升Java编程能力。
5.1 数组
Java中的数组是一个固定大小的同类型元素的容器。
数组声明与初始化
- 1// 声明并初始化一个整数数组
- 2int[] numbers = {1, 2, 3, 4, 5};
- 3System.out.println(numbers.length); // 输出数组长度:5
- 4
- 5// 分步声明和初始化
- 6int[] moreNumbers;
- 7moreNumbers = new int[10]; // 创建一个长度为10的整数数组
- 8moreNumbers[0] = 100;
遍历数组
- 1for (int i = 0; i < numbers.length; i++) {
- 2 System.out.println(numbers[i]);
- 3}
5.2 集合框架
Java集合框架提供了一组接口和类,用来代表和操纵各种集合。主要有List、Set和Map三大接口及其实现类。
ArrayList示例
- 1import java.util.ArrayList;
- 2
- 3ArrayList<String> names = new ArrayList<>();
- 4names.add("Alice");
- 5names.add("Bob");
- 6names.add("Charlie");
- 7
- 8// 遍历ArrayList
- 9for (String name : names) {
- 10 System.out.println(name);
- 11}
- 12
- 13// 查找元素
- 14if (names.contains("Bob")) {
- 15 System.out.println("Bob is in the list.");
- 16}
- 17
- 18// 删除元素
- 19names.remove("Bob");
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
HashSet示例
- 1import java.util.HashSet;
- 2
- 3HashSet<Integer> uniqueNumbers = new HashSet<>();
- 4uniqueNumbers.add(1);
- 5uniqueNumbers.add(2);
- 6uniqueNumbers.add(2); // 添加重复元素不会影响集合大小,因为HashSet不允许重复元素
- 7
- 8for (Integer number : uniqueNumbers) {
- 9 System.out.println(number);
- 10}
HashMap示例
- 1import java.util.HashMap;
- 2
- 3HashMap<String, Integer> studentGrades = new HashMap<>();
- 4studentGrades.put("Alice", 90);
- 5studentGrades.put("Bob", 85);
- 6
- 7// 获取学生分数
- 8int aliceGrade = studentGrades.get("Alice");
- 9System.out.println("Alice's grade: " + aliceGrade);
- 10
- 11// 判断是否存在某个键
- 12if (studentGrades.containsKey("Charlie")) {
- 13 System.out.println("Charlie exists in the map.");
- 14} else {
- 15 System.out.println("Charlie does not exist in the map.");
- 16}
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
通过理解和熟练使用Java数组和集合框架,程序员能够更好地管理和操作大量数据。后续章节将进一步探讨面向对象编程(OOP)的原理与实践,包括类与对象、封装、继承和多态等概念。
6.1 类与对象
在Java中,所有代码都是在类(Class)的上下文中定义的。类是对象的蓝图,而对象则是类的具体实例。
定义类与创建对象
- 1// 定义一个名为Person的类
- 2public class Person {
- 3 private String name;
- 4 private int age;
- 5
- 6 // 构造方法
- 7 public Person(String name, int age) {
- 8 this.name = name;
- 9 this.age = age;
- 10 }
- 11
- 12 // getter和setter方法
- 13 public String getName() {
- 14 return name;
- 15 }
- 16
- 17 public void setName(String name) {
- 18 this.name = name;
- 19 }
- 20
- 21 public int getAge() {
- 22 return age;
- 23 }
- 24
- 25 public void setAge(int age) {
- 26 this.age = age;
- 27 }
- 28}
- 29
- 30// 创建Person类的对象
- 31Person person = new Person("John Doe", 30);
- 32System.out.println(person.getName()); // 输出:John Doe
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
6.2 封装
封装是OOP的四大基本原则之一,它要求将数据(属性)和操作数据的方法绑定在一起,并隐藏内部实现细节。
上述Person
类就是一个封装的例子,其中的name
和age
属性是私有的(private),外部不能直接访问,而是通过getter和setter方法进行访问和修改。
6.3 继承
继承是OOP的另一个关键特性,允许一个类(子类)继承另一个类(父类)的属性和方法。
- 1// 定义一个Student类,继承自Person类
- 2public class Student extends Person {
- 3 private String major;
- 4
- 5 // 子类构造方法,调用父类构造方法
- 6 public Student(String name, int age, String major) {
- 7 super(name, age); // 调用父类Person的构造方法
- 8 this.major = major;
- 9 }
- 10
- 11 // Student类特有的方法
- 12 public String getMajor() {
- 13 return major;
- 14 }
- 15
- 16 public void setMajor(String major) {
- 17 this.major = major;
- 18 }
- 19}
- 20
- 21// 创建Student对象
- 22Student student = new Student("Jane Smith", 20, "Computer Science");
- 23System.out.println(student.getName()); // 输出:Jane Smith
- 24System.out.println(student.getMajor()); // 输出:Computer Science
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
6.4 多态
多态是指允许不同类的对象对同一消息作出响应,主要体现在方法重写(Overriding)和接口实现(Interface Implementation)上。
后续章节将进一步详细解释多态的原理与应用,并深入探讨抽象类、接口、内部类、枚举等Java面向对象编程的重要特性。通过掌握这些概念和技术,开发者可以设计出更具扩展性、更易维护的软件架构。
7.1 抽象类
抽象类是一种不能实例化的类,它主要用于描述一系列相关类所共有的行为特征。抽象类可以包含抽象方法(没有具体实现的方法)和具体方法(有实现的方法)。
抽象类示例
- 1abstract class Animal {
- 2 // 抽象方法
- 3 public abstract void makeSound();
- 4
- 5 // 具体方法
- 6 public void eat() {
- 7 System.out.println("Animal is eating.");
- 8 }
- 9}
- 10
- 11// 继承抽象类并实现抽象方法
- 12class Dog extends Animal {
- 13 @Override
- 14 public void makeSound() {
- 15 System.out.println("Woof Woof!");
- 16 }
- 17}
- 18
- 19public class Main {
- 20 public static void main(String[] args) {
- 21 Dog dog = new Dog();
- 22 dog.makeSound(); // 输出:Woof Woof!
- 23 dog.eat(); // 输出:Animal is eating.
- 24 }
- 25}
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
7.2 接口
接口是Java中的一种引用数据类型,它定义了一组方法签名,但不提供具体的实现。类可以通过实现接口来声明它们将支持一组特定的行为。
接口示例
- 1interface CanFly {
- 2 void fly();
- 3}
- 4
- 5class Bird implements CanFly {
- 6 @Override
- 7 public void fly() {
- 8 System.out.println("Bird is flying.");
- 9 }
- 10}
- 11
- 12public class Main {
- 13 public static void main(String[] args) {
- 14 Bird bird = new Bird();
- 15 bird.fly(); // 输出:Bird is flying.
- 16 }
- 17}
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
7.3 默认方法与静态方法(Java 8及以上)
从Java 8开始,接口可以包含默认方法(default method)和静态方法(static method)。
默认方法示例
- 1interface CanMove {
- 2 default void move() {
- 3 System.out.println("Default movement.");
- 4 }
- 5}
- 6
- 7class Car implements CanMove {
- 8 // 可以选择覆盖默认方法
- 9 @Override
- 10 public void move() {
- 11 System.out.println("Car is moving.");
- 12 }
- 13}
- 14
- 15public class Main {
- 16 public static void main(String[] args) {
- 17 CanMove car = new Car();
- 18 car.move(); // 输出:Car is moving.
- 19 }
- 20}
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
静态方法示例
- 1interface Calculator {
- 2 static int add(int a, int b) {
- 3 return a + b;
- 4 }
- 5}
- 6
- 7public class Main {
- 8 public static void main(String[] args) {
- 9 int sum = Calculator.add(10, 20);
- 10 System.out.println(sum); // 输出:30
- 11 }
- 12}
通过抽象类和接口,Java提供了灵活的设计方式,使得代码更容易复用和扩展。在后续章节中,我们将继续探索Java中的高级特性,包括枚举、注解、lambda表达式等,并结合实际案例分析如何在实际项目中有效地运用这些特性。
8.1 枚举
枚举类型在Java中是一种特殊的类,它可以有一系列预定义的实例,每个实例都是枚举类型的一个成员。枚举类型保证了实例的唯一性和类型安全性。
- 1public enum Color {
- 2 RED, GREEN, BLUE
- 3}
- 4
- 5public class Main {
- 6 public static void main(String[] args) {
- 7 Color color = Color.RED;
- 8 System.out.println(color); // 输出:RED
- 9 }
- 10}
枚举还可以定义方法和构造函数:
- 1public enum Color {
- 2 RED("FF0000"),
- 3 GREEN("00FF00"),
- 4 BLUE("0000FF");
- 5
- 6 private String hexCode;
- 7
- 8 Color(String hexCode) {
- 9 this.hexCode = hexCode;
- 10 }
- 11
- 12 public String getHexCode() {
- 13 return hexCode;
- 14 }
- 15}
- 16
- 17public class Main {
- 18 public static void main(String[] args) {
- 19 Color color = Color.BLUE;
- 20 System.out.println(color.getHexCode()); // 输出:"0000FF"
- 21 }
- 22}
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
8.2 注解
注解(Annotation)是一种元数据,它为我们在代码中添加信息提供了途径,这些信息可以被编译器解析,也可以被运行时环境使用。注解不会改变程序的行为,但可以被编译器和其他工具用来生成代码、XML文件,或者做验证和处理。
自定义注解示例
- 1import java.lang.annotation.ElementType;
- 2import java.lang.annotation.Retention;
- 3import java.lang.annotation.RetentionPolicy;
- 4import java.lang.annotation.Target;
- 5
- 6// 定义一个注解
- 7@Retention(RetentionPolicy.RUNTIME) // 表示注解在运行时可用
- 8@Target(ElementType.METHOD) // 表示注解可以应用于方法级别
- 9@interface Testable {
- 10 String author() default "Unknown"; // 注解参数
- 11}
- 12
- 13public class Main {
- 14 @Testable(author = "John Doe")
- 15 public void testMethod() {
- 16 // ...
- 17 }
- 18
- 19 public static void main(String[] args) {
- 20 // 获取注解信息
- 21 Method method = Main.class.getMethod("testMethod");
- 22 Testable annotation = method.getAnnotation(Testable.class);
- 23 System.out.println(annotation.author()); // 输出:"John Doe"
- 24 }
- 25}
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
通过枚举和注解,Java增强了代码的清晰度和可维护性,同时也为编译器和工具提供了更多元数据支持,便于进行自动化处理和验证。在接下来的章节中,我们将探讨Java 8引入的Lambda表达式、函数式接口、Stream API等特性,进一步提升代码的简洁性和表达能力。
9.1 Lambda表达式
Java 8 引入了Lambda表达式作为函数式编程的基础,使得代码更加简洁且易于理解。Lambda表达式允许我们将行为(行为即实现了某个接口的方法体)作为变量来传递,简化了匿名内部类的创建过程。
Lambda表达式基础示例
- 1import java.util.Arrays;
- 2import java.util.List;
- 3import java.util.function.Predicate;
- 4
- 5public class Main {
- 6 public static void main(String[] args) {
- 7 List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David");
- 8
- 9 // 使用Lambda表达式实现过滤操作
- 10 Predicate<String> startsWithB = (s) -> s.startsWith("B");
- 11 names.stream()
- 12 .filter(startsWithB)
- 13 .forEach(System.out::println);
- 14
- 15 // 等同于匿名内部类形式
- 16 // Predicate<String> startsWithB = new Predicate<String>() {
- 17 // @Override
- 18 // public boolean test(String s) {
- 19 // return s.startsWith("B");
- 20 // }
- 21 // };
- 22 }
- 23}
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
9.2 函数式接口
为了支持Lambda表达式的使用,Java 8引入了一个新的概念——函数式接口(Functional Interface),它只有一个抽象方法的接口。java.util.function
包下包含了许多这样的接口,如 Predicate<T>
、Function<T, R>
、Consumer<T>
等。
函数式接口示例
- 1import java.util.ArrayList;
- 2import java.util.List;
- 3import java.util.function.Function;
- 4
- 5public class Main {
- 6 public static void main(String[] args) {
- 7 List<Integer> numbers = new ArrayList<>();
- 8 numbers.add(10);
- 9 numbers.add(20);
- 10 numbers.add(30);
- 11
- 12 // 使用Function接口的Lambda表达式将整数转换为字符串
- 13 Function<Integer, String> toStr = Integer::toString;
- 14 List<String> strings = numbers.stream().map(toStr).collect(Collectors.toList());
- 15 System.out.println(strings); // 输出:[10, 20, 30]
- 16
- 17 // 等同于匿名内部类形式
- 18 // Function<Integer, String> toStr = new Function<Integer, String>() {
- 19 // @Override
- 20 // public String apply(Integer i) {
- 21 // return i.toString();
- 22 // }
- 23 // };
- 24 }
- 25}
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
Java 8还引入了Stream
API,结合Lambda表达式,能够极大地简化集合操作以及提高代码的可读性与效率。在后续章节中,将进一步探讨Stream
API及其高级应用。
10.1 Stream API概述
Java 8 引入了 Stream API,这是一种用于处理数据流的新方法,特别适合进行批量数据处理和聚合操作。Stream API 可以配合 Lambda 表达式,让代码更为简洁且具备函数式编程风格。
创建并使用Stream API示例
- 1import java.util.Arrays;
- 2import java.util.List;
- 3import java.util.stream.Collectors;
- 4
- 5public class Main {
- 6 public static void main(String[] args) {
- 7 List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
- 8
- 9 // 使用Stream API过滤奇数并求和
- 10 long sumOfOddNumbers = numbers.stream()
- 11 .filter(n -> n % 2 != 0)
- 12 .mapToInt(Integer::intValue)
- 13 .sum();
- 14 System.out.println("Sum of odd numbers: " + sumOfOddNumbers); // 输出:25
- 15
- 16 // 使用Stream API收集到一个新的List
- 17 List<String> squaredStrings = numbers.stream()
- 18 .map(n -> String.valueOf(n * n))
- 19 .collect(Collectors.toList());
- 20 System.out.println(squaredStrings); // 输出:["1", "4", "9", "16", "25", "36", "49", "64", "81"]
- 21 }
- 22}
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
10.2 Stream API常用操作
创建Stream: 通过 Collection 的 stream() 或 parallelStream() 方法获取流。
中间操作: 如 filter(), map(), sorted() 等,中间操作不会执行任何处理,直到遇到终止操作。
终止操作: 如 count(), collect(), forEach() 等,执行中间操作链,并产生最终结果或副作用。
短路操作: 诸如 findFirst() 和 anyMatch() 之类的操作可以在找到符合条件的结果后立即停止处理流。
并行流: 通过 parallelStream() 方法创建的流可以在多个线程间并行处理,从而提高大数据集的处理速度。
通过 Stream API,Java 开发者可以利用函数式编程的优势,写出简洁高效的代码。在后续章节中,我们将更深入地探讨 Stream API 的高级特性和应用场景,包括组合流、收集器、并行流处理等。
11.1 线程与Runnable接口
Java中创建线程有两种常见方式,一种是通过实现Runnable
接口,另一种是继承Thread
类。下面是一个使用Runnable
接口创建线程的示例:
- 1public class MyRunnable implements Runnable {
- 2 @Override
- 3 public void run() {
- 4 for (int i = 0; i < 10; i++) {
- 5 System.out.println(Thread.currentThread().getName() + ": " + i);
- 6 }
- 7 }
- 8
- 9 public static void main(String[] args) {
- 10 Thread thread = new Thread(new MyRunnable());
- 11 thread.start();
- 12
- 13 // 主线程也打印一些信息
- 14 for (int i = 0; i < 10; i++) {
- 15 System.out.println("Main Thread: " + i);
- 16 }
- 17 }
- 18}
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
11.2 Thread类
直接继承Thread
类创建线程如下:
- 1public class MyThread extends Thread {
- 2 @Override
- 3 public void run() {
- 4 for (int i = 0; i < 10; i++) {
- 5 System.out.println(this.getName() + ": " + i);
- 6 }
- 7 }
- 8
- 9 public static void main(String[] args) {
- 10 MyThread thread = new MyThread();
- 11 thread.setName("My Custom Thread");
- 12 thread.start();
- 13
- 14 // 主线程同样打印信息
- 15 for (int i = 0; i < 10; i++) {
- 16 System.out.println("Main Thread: " + i);
- 17 }
- 18 }
- 19}
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
11.3 同步与锁
在多线程环境下,为了避免资源共享时的数据竞争问题,Java提供了多种同步机制。最常用的同步手段就是synchronized
关键字和ReentrantLock
等锁类。
- 1public class Counter {
- 2 private int count = 0;
- 3 private final Object lock = new Object();
- 4
- 5 public void increment() {
- 6 synchronized (lock) {
- 7 count++;
- 8 }
- 9 }
- 10
- 11 public int getCount() {
- 12 synchronized (lock) {
- 13 return count;
- 14 }
- 15 }
- 16
- 17 public static void main(String[] args) {
- 18 Counter counter = new Counter();
- 19 Thread t1 = new Thread(() -> {
- 20 for (int i = 0; i < 10000; i++) {
- 21 counter.increment();
- 22 }
- 23 });
- 24
- 25 Thread t2 = new Thread(() -> {
- 26 for (int i = 0; i < 10000; i++) {
- 27 counter.increment();
- 28 }
- 29 });
- 30
- 31 t1.start();
- 32 t2.start();
- 33
- 34 try {
- 35 t1.join();
- 36 t2.join();
- 37 } catch (InterruptedException e) {
- 38 e.printStackTrace();
- 39 }
- 40
- 41 System.out.println("Final count: " + counter.getCount());
- 42 }
- 43}
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
通过深入了解Java并发编程与多线程技术,可以更好地优化应用程序性能,提高资源利用率。在后续章节中,我们将深入探讨Java并发编程中的高级话题,如线程池、Future与Callable、原子变量类以及并发容器等。
12.1 ExecutorService与线程池
Java的java.util.concurrent
包提供了一系列并发工具类,其中包括线程池的实现ExecutorService
。通过使用线程池,可以有效地管理和控制线程的数量,避免频繁创建和销毁线程带来的性能开销。
- 1import java.util.concurrent.ExecutorService;
- 2import java.util.concurrent.Executors;
- 3
- 4public class ThreadPoolExample {
- 5
- 6 public static void main(String[] args) {
- 7 // 创建一个固定大小的线程池
- 8 ExecutorService executor = Executors.newFixedThreadPool(5);
- 9
- 10 for (int i = 0; i < 10; i++) {
- 11 Runnable worker = () -> {
- 12 System.out.println(Thread.currentThread().getName() + "正在工作");
- 13 try {
- 14 Thread.sleep(500);
- 15 } catch (InterruptedException e) {
- 16 e.printStackTrace();
- 17 }
- 18 };
- 19
- 20 // 提交任务给线程池
- 21 executor.execute(worker);
- 22 }
- 23
- 24 // 关闭线程池,不再接受新任务并等待所有已提交的任务完成
- 25 executor.shutdown();
- 26 while (!executor.isTerminated()) {
- 27 }
- 28
- 29 System.out.println("所有任务已完成,线程池已关闭");
- 30 }
- 31}
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
12.2 Future与Callable
除了Runnable
接口,Java还提供了Callable
接口,它允许线程执行任务并返回结果。配合Future
接口,可以获取线程执行的结果或取消任务。
- 1import java.util.concurrent.Callable;
- 2import java.util.concurrent.ExecutorService;
- 3import java.util.concurrent.Executors;
- 4import java.util.concurrent.Future;
- 5
- 6public class CallableExample {
- 7
- 8 public static void main(String[] args) throws Exception {
- 9 ExecutorService executor = Executors.newSingleThreadExecutor();
- 10
- 11 Future<Integer> future = executor.submit(new Callable<Integer>() {
- 12 @Override
- 13 public Integer call() throws Exception {
- 14 int sum = 0;
- 15 for (int i = 0; i < 100; i++) {
- 16 sum += i;
- 17 }
- 18 return sum;
- 19 }
- 20 });
- 21
- 22 // 执行其他任务...
- 23
- 24 // 获取线程计算的结果
- 25 Integer result = future.get();
- 26 System.out.println("1到100的和是: " + result);
- 27
- 28 // 关闭线程池
- 29 executor.shutdown();
- 30 }
- 31}
- 32
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
通过了解并熟练使用Java的线程池和并发工具类,可以大幅提升并发编程的效率和可控性。在后续章节中,我们将继续探讨更高级的并发控制机制,如信号量、栅栏、条件变量以及并发容器等。
13.1 ConcurrentHashMap
在并发环境下,Java提供了一种线程安全的哈希表实现——ConcurrentHashMap
,它允许多个线程安全地进行读写操作。
- 1import java.util.concurrent.ConcurrentHashMap;
- 2
- 3public class ConcurrentHashMapExample {
- 4 public static void main(String[] args) {
- 5 ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();
- 6
- 7 // 多线程环境下安全地插入数据
- 8 map.put("Apple", 1);
- 9 map.put("Banana", 2);
- 10
- 11 // 多线程环境下安全地读取数据
- 12 System.out.println(map.get("Apple")); // 输出:1
- 13
- 14 // 更新数据
- 15 map.computeIfPresent("Apple", (key, oldValue) -> oldValue + 1);
- 16 System.out.println(map.get("Apple")); // 输出:2
- 17 }
- 18}
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
13.2 CopyOnWriteArrayList
CopyOnWriteArrayList
是一个线程安全的列表,它在写操作时会复制整个底层数组,因此适合读多写少的并发场景。
- 1import java.util.Iterator;
- 2import java.util.concurrent.CopyOnWriteArrayList;
- 3
- 4public class CopyOnWriteArrayListExample {
- 5 public static void main(String[] args) {
- 6 CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<>();
- 7 list.add("Apple");
- 8 list.add("Banana");
- 9
- 10 // 多线程环境下安全地遍历
- 11 new Thread(() -> {
- 12 for (String item : list) {
- 13 System.out.println(item);
- 14 }
- 15 }).start();
- 16
- 17 // 在遍历的同时添加元素,不会抛出ConcurrentModificationException
- 18 list.add("Cherry");
- 19
- 20 // 等待子线程完成
- 21 // (这里实际生产环境中需要合理控制线程同步和结束)
- 22 }
- 23}
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
Java并发容器为高并发场景下的数据共享提供了便利,通过使用这些容器,可以减少因并发控制导致的额外开销,提高代码执行效率和健壮性。后续章节将进一步探讨Java并发容器的其他实现,如BlockingQueue、Semaphore等,并讲解如何在实际项目中合理运用它们。
14.1 ScheduledExecutorService
Java通过ScheduledExecutorService
接口提供了定时任务的功能,可以按指定的时间间隔周期性地执行任务或延时执行一次性任务。
- 1import java.util.concurrent.Executors;
- 2import java.util.concurrent.ScheduledExecutorService;
- 3import java.util.concurrent.TimeUnit;
- 4
- 5public class ScheduledExecutorServiceExample {
- 6 public static void main(String[] args) {
- 7 ScheduledExecutorService scheduledExecutor = Executors.newScheduledThreadPool(1);
- 8
- 9 // 延时5秒后执行一次性任务
- 10 scheduledExecutor.schedule(() -> {
- 11 System.out.println("延时5秒后执行:一次性任务");
- 12 }, 5, TimeUnit.SECONDS);
- 13
- 14 // 每隔2秒执行一次周期性任务
- 15 scheduledExecutor.scheduleAtFixedRate(() -> {
- 16 System.out.println("每隔2秒执行一次:周期性任务");
- 17 }, 0, 2, TimeUnit.SECONDS);
- 18
- 19 // 关闭线程池(这里仅作演示,实际应确保所有任务都完成后再关闭)
- 20 // scheduledExecutor.shutdown();
- 21 }
- 22}
- 23
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
14.2 Timer与TimerTask
虽然ScheduledExecutorService
是首选的定时任务解决方案,但Java早期版本中提供的Timer
类也能实现类似功能。
- 1import java.util.Timer;
- 2import java.util.TimerTask;
- 3
- 4public class TimerExample {
- 5 public static void main(String[] args) {
- 6 Timer timer = new Timer();
- 7
- 8 // 延时5秒后执行一次性任务
- 9 timer.schedule(new TimerTask() {
- 10 @Override
- 11 public void run() {
- 12 System.out.println("延时5秒后执行:一次性任务");
- 13 }
- 14 }, 5000);
- 15
- 16 // 每隔2秒执行一次周期性任务
- 17 timer.scheduleAtFixedRate(new TimerTask() {
- 18 @Override
- 19 public void run() {
- 20 System.out.println("每隔2秒执行一次:周期性任务");
- 21 }
- 22 }, 0, 2000);
- 23
- 24 // 关闭计时器(这里仅作演示,实际应确保所有任务都完成后再调用cancel方法)
- 25 // timer.cancel();
- 26 }
- 27}
- 28
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
通过使用Java的定时任务工具类,我们可以轻松地实现各种定时或周期性的后台任务,增强程序的自动化程度和实时响应能力。在后续章节中,我们将进一步讨论如何在实际项目中设计和管理复杂的定时任务,以及相关的调度策略和性能优化措施。
15.1 文件读写操作
Java提供了丰富的I/O流类库,可以方便地进行文件读写操作。以下是一些基本的文件读写示例:
写入文件
- 1import java.io.FileWriter;
- 2import java.io.IOException;
- 3
- 4public class WriteToFileExample {
- 5 public static void main(String[] args) {
- 6 String content = "This is a sample text written to a file.";
- 7
- 8 try (FileWriter writer = new FileWriter("output.txt")) {
- 9 writer.write(content);
- 10 } catch (IOException e) {
- 11 e.printStackTrace();
- 12 }
- 13 }
- 14}
读取文件
- 1import java.io.BufferedReader;
- 2import java.io.FileReader;
- 3import java.io.IOException;
- 4
- 5public class ReadFromFileExample {
- 6 public static void main(String[] args) {
- 7 try (BufferedReader reader = new BufferedReader(new FileReader("input.txt"))) {
- 8 String line;
- 9 while ((line = reader.readLine()) != null) {
- 10 System.out.println(line);
- 11 }
- 12 } catch (IOException e) {
- 13 e.printStackTrace();
- 14 }
- 15 }
- 16}
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
15.2 字节流与字符流
Java I/O流分为字节流和字符流两大类。字节流处理原始字节数据,字符流则处理字符数据,自动处理字符编码。
字节流示例:
- 1import java.io.FileInputStream;
- 2import java.io.FileOutputStream;
- 3import java.io.IOException;
- 4
- 5public class ByteStreamExample {
- 6 public static void main(String[] args) {
- 7 byte[] buffer = new byte[1024];
- 8 try (FileInputStream fis = new FileInputStream("binaryfile.bin");
- 9 FileOutputStream fos = new FileOutputStream("copiedfile.bin")) {
- 10
- 11 int readBytes;
- 12 while ((readBytes = fis.read(buffer)) != -1) {
- 13 fos.write(buffer, 0, readBytes);
- 14 }
- 15 } catch (IOException e) {
- 16 e.printStackTrace();
- 17 }
- 18 }
- 19}
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
字符流示例:
- 1import java.io.FileReader;
- 2import java.io.FileWriter;
- 3import java.io.IOException;
- 4
- 5public class CharStreamExample {
- 6 public static void main(String[] args) {
- 7 try (FileReader reader = new FileReader("textfile.txt");
- 8 FileWriter writer = new FileWriter("copytextfile.txt")) {
- 9
- 10 int c;
- 11 while ((c = reader.read()) != -1) {
- 12 writer.write(c);
- 13 }
- 14 } catch (IOException e) {
- 15 e.printStackTrace();
- 16 }
- 17 }
- 18}
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
通过掌握Java的I/O流操作,开发者能够有效地处理磁盘文件和网络数据,进行高效的数据交换。在后续章节中,我们将进一步探讨I/O流的高级特性,如缓冲流、转换流、对象序列化与反序列化等。
16.1 TCP Socket通信
TCP(Transmission Control Protocol)是一种面向连接的、可靠的传输协议。在Java中,我们可以使用Socket
和ServerSocket
类实现TCP通信。
服务器端(ServerSocket)示例:
- 1import java.io.*;
- 2import java.net.ServerSocket;
- 3import java.net.Socket;
- 4
- 5public class Server {
- 6 public static void main(String[] args) throws IOException {
- 7 ServerSocket serverSocket = new ServerSocket(8080); // 创建ServerSocket监听8080端口
- 8
- 9 System.out.println("Server started...");
- 10 while (true) {
- 11 Socket socket = serverSocket.accept(); // 阻塞等待客户端连接请求
- 12
- 13 new Thread(() -> {
- 14 try (
- 15 PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
- 16 BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()))
- 17 ) {
- 18 String inputLine;
- 19 while ((inputLine = in.readLine()) != null) {
- 20 System.out.println("Received message: " + inputLine);
- 21 out.println("Echo: " + inputLine);
- 22 }
- 23 } catch (IOException e) {
- 24 e.printStackTrace();
- 25 }
- 26 }).start();
- 27 }
- 28 }
- 29}
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
客户端(Socket)示例:
- 1import java.io.*;
- 2import java.net.Socket;
- 3
- 4public class Client {
- 5 public static void main(String[] args) throws IOException {
- 6 Socket socket = new Socket("localhost", 8080); // 连接到本地主机的8080端口
- 7
- 8 try (
- 9 PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
- 10 BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()))
- 11 ) {
- 12 BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
- 13 String userInput;
- 14
- 15 while ((userInput = stdIn.readLine()) != null) {
- 16 out.println(userInput); // 发送消息给服务器
- 17 System.out.println("Server response: " + in.readLine()); // 读取服务器回复的消息
- 18 }
- 19 } finally {
- 20 socket.close(); // 关闭连接
- 21 }
- 22 }
- 23}
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
以上代码分别展示了如何在Java中创建一个简单的TCP服务器和客户端,实现双向通信。
16.2 UDP DatagramSocket通信
UDP(User Datagram Protocol)是一种无连接的、不可靠的传输协议。在Java中,我们使用DatagramSocket
和DatagramPacket
类实现UDP通信。
发送端(DatagramSocket)示例:
- 1import java.io.IOException;
- 2import java.net.DatagramPacket;
- 3import java.net.DatagramSocket;
- 4import java.net.InetAddress;
- 5
- 6public class UdpSender {
- 7 public static void main(String[] args) throws IOException {
- 8 String message = "Hello from UDP sender!";
- 9 byte[] buf = message.getBytes();
- 10
- 11 InetAddress address = InetAddress.getByName("localhost");
- 12 int port = 9000;
- 13
- 14 DatagramSocket socket = new DatagramSocket();
- 15 DatagramPacket packet = new DatagramPacket(buf, buf.length, address, port);
- 16
- 17 socket.send(packet);
- 18 System.out.println("Sent: " + message);
- 19
- 20 socket.close();
- 21 }
- 22}
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
接收端(DatagramSocket)示例:
- 1import java.io.IOException;
- 2import java.net.DatagramPacket;
- 3import java.net.DatagramSocket;
- 4
- 5public class UdpReceiver {
- 6 public static void main(String[] args) throws IOException {
- 7 byte[] receiveData = new byte[1024];
- 8 DatagramSocket socket = new DatagramSocket(9000);
- 9
- 10 while (true) {
- 11 DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
- 12 socket.receive(receivePacket);
- 13
- 14 String receivedMessage = new String(receivePacket.getData()).trim();
- 15 System.out.println("Received: " + receivedMessage);
- 16 }
- 17 }
- 18}
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
以上代码分别展示了如何在Java中创建一个简单的UDP发送端和接收端,实现单向消息传输。在实际应用中,通常需要在接收端也发送确认消息,以便建立双向通信。在后续章节中,我们将进一步探讨Java网络编程的高级主题,如NIO(非阻塞I/O)、SSL/TLS加密通信等。
17.1 Java NIO简介
Java NIO(New Input/Output)是在Java 1.4版本引入的,提供了一种替代标准I/O的高性能、可伸缩的选择。NIO的主要特点是基于通道(Channel)和缓冲区(Buffer)进行数据处理,同时支持非阻塞I/O操作。
17.2 Channel与Buffer
Channel示例:
- 1import java.io.RandomAccessFile;
- 2import java.nio.channels.FileChannel;
- 3
- 4public class NioChannelExample {
- 5 public static void main(String[] args) throws Exception {
- 6 RandomAccessFile file = new RandomAccessFile("data.txt", "rw");
- 7 FileChannel channel = file.getChannel();
- 8
- 9 // 从Channel读取数据到Buffer
- 10 ByteBuffer buffer = ByteBuffer.allocate(48);
- 11 int bytesRead = channel.read(buffer);
- 12 buffer.flip();
- 13
- 14 while (buffer.hasRemaining()) {
- 15 System.out.print((char) buffer.get());
- 16 }
- 17
- 18 file.close();
- 19 }
- 20}
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
Buffer示例:
- 1import java.nio.ByteBuffer;
- 2
- 3public class NioBufferExample {
- 4 public static void main(String[] args) {
- 5 ByteBuffer buffer = ByteBuffer.allocate(10);
- 6
- 7 // 写入数据
- 8 buffer.put("Hello NIO!".getBytes());
- 9 buffer.flip();
- 10
- 11 // 读取数据
- 12 while (buffer.hasRemaining()) {
- 13 System.out.print((char) buffer.get());
- 14 }
- 15 }
- 16}
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
17.3 Selector与非阻塞I/O
Selector是Java NIO的核心组件,允许单个线程管理多个通道。Selector可以监控多个通道的事件状态(如读就绪、写就绪),从而实现非阻塞I/O操作。
Selector示例:
- 1import java.io.IOException;
- 2import java.net.InetSocketAddress;
- 3import java.nio.ByteBuffer;
- 4import java.nio.channels.SelectionKey;
- 5import java.nio.channels.Selector;
- 6import java.nio.channels.ServerSocketChannel;
- 7import java.nio.channels.SocketChannel;
- 8
- 9public class NioSelectorExample {
- 10 public static void main(String[] args) throws IOException {
- 11 ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
- 12 serverSocketChannel.configureBlocking(false);
- 13 serverSocketChannel.socket().bind(new InetSocketAddress(8080));
- 14
- 15 Selector selector = Selector.open();
- 16 serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
- 17
- 18 while (true) {
- 19 selector.select();
- 20
- 21 Iterator<SelectionKey> keys = selector.selectedKeys().iterator();
- 22 while (keys.hasNext()) {
- 23 SelectionKey key = keys.next();
- 24 keys.remove();
- 25
- 26 if (key.isAcceptable()) {
- 27 SocketChannel clientSocketChannel = serverSocketChannel.accept();
- 28 clientSocketChannel.configureBlocking(false);
- 29 clientSocketChannel.register(selector, SelectionKey.OP_READ);
- 30 } else if (key.isReadable()) {
- 31 // 读取数据并处理
- 32 // ...
- 33 }
- 34 }
- 35 }
- 36 }
- 37}
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
通过Java NIO,开发者可以构建高性能、可伸缩的网络应用程序,尤其是在处理大量并发连接时优势明显。在后续章节中,我们将进一步探讨Java NIO的高级特性,如内存映射文件、管道等,并学习如何在实际项目中有效利用NIO进行复杂的数据传输和处理。
18.1 异常的基本概念
在Java中,异常是对程序运行时错误的处理机制。当程序出现异常情况时,系统会抛出一个异常对象,该对象可以被捕获并进行适当的处理。
简单异常处理示例:
- 1public class ExceptionHandlingExample {
- 2 public static void main(String[] args) {
- 3 try {
- 4 divideByZero();
- 5 } catch (ArithmeticException e) {
- 6 System.out.println("捕获到了除以零的异常: " + e.getMessage());
- 7 }
- 8 }
- 9
- 10 public static void divideByZero() {
- 11 int denominator = 0;
- 12 int result = 10 / denominator; // 这将抛出ArithmeticException
- 13 }
- 14}
18.2 自定义异常
Java允许开发者自定义异常,通过继承自Exception
或其子类来创建自己的异常类。
自定义异常示例:
- 1class InvalidUsernameException extends Exception {
- 2 public InvalidUsernameException(String message) {
- 3 super(message);
- 4 }
- 5}
- 6
- 7public class CustomExceptionExample {
- 8 public static void main(String[] args) {
- 9 try {
- 10 validateUsername("invalid#username");
- 11 } catch (InvalidUsernameException e) {
- 12 System.out.println("用户名无效: " + e.getMessage());
- 13 }
- 14 }
- 15
- 16 public static void validateUsername(String username) throws InvalidUsernameException {
- 17 if (!username.matches("[a-zA-Z0-9]+")) {
- 18 throw new InvalidUsernameException("用户名只能包含字母和数字");
- 19 }
- 20 }
- 21}
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
18.3 Finally块和Try-with-resources语句
无论是否发生异常,finally块中的代码总会被执行。此外,Java 7引入了try-with-resources语句,用于自动关闭实现了AutoCloseable接口的资源。
Finally块示例:
- 1public class FinallyBlockExample {
- 2 public static void main(String[] args) {
- 3 try {
- 4 File file = new File("example.txt");
- 5 FileReader fr = new FileReader(file);
- 6 // ... 进行读取操作
- 7 } catch (FileNotFoundException e) {
- 8 e.printStackTrace();
- 9 } finally {
- 10 try {
- 11 if (fr != null) {
- 12 fr.close(); // 确保文件读取流关闭
- 13 }
- 14 } catch (IOException ex) {
- 15 ex.printStackTrace();
- 16 }
- 17 }
- 18 }
- 19}
- 20
- 21// 使用Try-with-resources语句简化资源关闭操作
- 22public class TryWithResourcesExample {
- 23 public static void main(String[] args) {
- 24 try (FileReader fr = new FileReader("example.txt")) {
- 25 // ... 进行读取操作
- 26 } catch (FileNotFoundException e) {
- 27 e.printStackTrace();
- 28 } catch (IOException e) {
- 29 e.printStackTrace();
- 30 }
- 31 // 不再需要手动关闭fr,因为try-with-resources会自动关闭它
- 32 }
- 33}
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
通过有效的异常处理,Java程序员可以编写出更加健壮和鲁棒的应用程序,能够在出现问题时提供有用的反馈信息,并采取相应的恢复措施。后续章节将继续深入介绍异常处理的最佳实践以及在复杂业务逻辑中如何更好地应用异常处理机制。
19.1 反射的基本概念
Java反射API允许我们在运行时检查类、接口、字段和方法的信息,并能动态地创建和操作对象。这为我们提供了极大的灵活性,特别是在处理未知类型的对象或实现框架及插件系统时。
反射获取类信息示例:
- 1import java.lang.reflect.Field;
- 2import java.lang.reflect.Method;
- 3
- 4public class ReflectionExample {
- 5 public static void main(String[] args) {
- 6 try {
- 7 Class<?> stringClass = Class.forName("java.lang.String");
- 8 System.out.println("类名: " + stringClass.getName());
- 9 System.out.println("简单类名: " + stringClass.getSimpleName());
- 10
- 11 // 获取所有公共字段
- 12 Field[] fields = stringClass.getFields();
- 13 for (Field field : fields) {
- 14 System.out.println("字段名: " + field.getName());
- 15 }
- 16
- 17 // 获取所有公共方法
- 18 Method[] methods = stringClass.getMethods();
- 19 for (Method method : methods) {
- 20 System.out.println("方法名: " + method.getName());
- 21 }
- 22 } catch (ClassNotFoundException e) {
- 23 e.printStackTrace();
- 24 }
- 25 }
- 26}
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
19.2 动态创建对象和调用方法
通过反射API,我们可以在运行时根据类名创建对象,并调用其方法或访问其字段。
动态创建对象和调用方法示例:
- 1import java.lang.reflect.Constructor;
- 2import java.lang.reflect.InvocationTargetException;
- 3
- 4public class ReflectionCreateAndInvokeExample {
- 5 public static void main(String[] args) {
- 6 try {
- 7 // 获取String类的构造方法
- 8 Class<?> stringClass = Class.forName("java.lang.String");
- 9 Constructor<?> constructor = stringClass.getConstructor(char[].class);
- 10
- 11 // 动态创建对象
- 12 char[] chars = {'H', 'e', 'l', 'l', 'o'};
- 13 Object instance = constructor.newInstance(chars);
- 14
- 15 // 调用方法
- 16 Method method = stringClass.getMethod("length");
- 17 int length = (int) method.invoke(instance);
- 18 System.out.println("字符串长度: " + length);
- 19
- 20 // 访问字段
- 21 Field valueField = stringClass.getDeclaredField("value");
- 22 valueField.setAccessible(true); // 设置为可访问私有字段
- 23 char[] value = (char[]) valueField.get(instance);
- 24 System.out.println("字符串内容: " + new String(value));
- 25 } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException |
- 26 InstantiationException | InvocationTargetException | NoSuchFieldException e) {
- 27 e.printStackTrace();
- 28 }
- 29 }
- 30}
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
尽管反射API赋予了Java强大的动态性,但它也有一些潜在的风险,如性能下降、破坏封装等。在实际开发中,应谨慎使用反射,并充分考虑其对程序的影响。后续章节将探讨反射在实际应用场景中的最佳实践以及与其他Java特性结合使用的例子。
20.1 注解处理器概述
Java注解处理器允许开发者在编译时扫描源代码中的注解,并根据注解信息生成额外的源代码或辅助文件。注解处理器的工作原理是在编译阶段触发,它能够帮助开发者自动化一些重复性的工作,比如生成样板代码、验证代码规范、自动生成配置文件等。
20.2 自定义注解与注解处理器
首先,我们定义一个自定义注解:
- 1import java.lang.annotation.ElementType;
- 2import java.lang.annotation.Retention;
- 3import java.lang.annotation.RetentionPolicy;
- 4import java.lang.annotation.Target;
- 5
- 6@Retention(RetentionPolicy.SOURCE)
- 7@Target(ElementType.TYPE)
- 8public @interface GenerateToString {
- 9 boolean includeFields() default true;
- 10}
然后,我们创建一个注解处理器:
- 1import javax.annotation.processing.AbstractProcessor;
- 2import javax.annotation.processing.RoundEnvironment;
- 3import javax.lang.model.SourceVersion;
- 4import javax.lang.model.element.TypeElement;
- 5import javax.tools.JavaFileObject;
- 6import java.io.IOException;
- 7import java.io.Writer;
- 8import java.util.Set;
- 9
- 10public class ToStringProcessor extends AbstractProcessor {
- 11 @Override
- 12 public Set<String> getSupportedAnnotationTypes() {
- 13 return Set.of(GenerateToString.class.getName());
- 14 }
- 15
- 16 @Override
- 17 public SourceVersion getSupportedSourceVersion() {
- 18 return SourceVersion.latestSupported();
- 19 }
- 20
- 21 @Override
- 22 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
- 23 for (TypeElement annotation : annotations) {
- 24 for (Element element : roundEnv.getElementsAnnotatedWith(annotation)) {
- 25 if (element instanceof TypeElement) {
- 26 TypeElement typeElement = (TypeElement) element;
- 27 GenerateToString annotationInstance = typeElement.getAnnotation(GenerateToString.class);
- 28 generateToString(typeElement, annotationInstance.includeFields());
- 29 }
- 30 }
- 31 }
- 32 return true;
- 33 }
- 34
- 35 private void generateToString(TypeElement typeElement, boolean includeFields) {
- 36 // 在此处实现生成toString方法的逻辑
- 37 // ...
- 38
- 39 // 创建Java源码文件
- 40 String className = typeElement.getSimpleName().toString();
- 41 String packageName = processingEnv.getElementUtils().getPackageOf(typeElement).getQualifiedName().toString();
- 42 String fqClassName = packageName.isEmpty() ? className : packageName + "." + className;
- 43
- 44 try (JavaFileObject jfo = processingEnv.getFiler().createSourceFile(fqClassName + "Generated")) {
- 45 Writer writer = jfo.openWriter();
- 46 writer.append("// Autogenerated toString() method by ToStringProcessor\n");
- 47 // 将生成的toString()方法源码写入writer
- 48 writer.close();
- 49 } catch (IOException e) {
- 50 e.printStackTrace();
- 51 }
- 52 }
- 53}
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
在上述代码中,我们定义了一个名为GenerateToString
的注解,并创建了一个对应的注解处理器ToStringProcessor
。在注解处理器中,我们检查哪些类使用了GenerateToString
注解,并针对这些类生成toString()
方法的源代码。
要使注解处理器生效,还需要在META-INF/services/javax.annotation.processing.Processor
文件中声明处理器类名,并将处理器项目添加到主项目的构建路径中。
Java注解处理器是一项强大且灵活的技术,可用于构建各种编译时代码生成和验证工具,大大提高开发效率和代码质量。在后续章节中,我们将详细说明如何打包和部署注解处理器,以及如何处理更复杂的注解场景。
本章介绍了Java注解处理器这一强大的工具,它能够在编译时处理源代码中的注解并据此生成额外的代码或执行其他操作。首先,我们展示了如何定义一个简单的自定义注解GenerateToString
,该注解可以标记在类上,指示注解处理器为该类生成toString()
方法。
接下来,我们创建了一个继承自javax.annotation.processing.AbstractProcessor
的注解处理器类ToStringProcessor
。此处理器通过重写相关方法来指定支持的注解类型、源代码版本,并在process()
方法中实际处理带有特定注解的元素。当检测到类被GenerateToString
注解时,generateToString()
方法会被调用来生成对应的toString()
方法源代码。
为了最终应用这个注解处理器,需要将其注册到META-INF/services/javax.annotation.processing.Processor
文件中,并确保正确配置项目的构建路径,以便在编译过程中自动运行注解处理器。
总之,Java注解处理器是一个强大的机制,用于扩展Java编译器的能力,在编译时完成诸如代码生成、验证或其他自定义处理任务,从而简化开发流程,减少手动编写重复代码的工作量。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。