
主要用于表示匿名函数
语法:
(parameters) -> { statements; }
或
(parameters) -> expression
->:lambda 运算符
接口是一种抽象类型,定义了类必须实现的方法。接口中的所有方法默认都是抽象的(没有方法体),且所有字段默认都是 public static final。
interface Animal {
void eat();
void sleep();
}
class Dog implements Animal {
@Override
public void eat() {
System.out.println("Dog eats");
}
@Override
public void sleep() {
System.out.println("Dog sleeps");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat();
dog.sleep();
}
}
抽象类是不能被实例化的类,可以包含抽象方法和具体方法。抽象方法必须在子类中实现。
abstract class Animal {
abstract void makeSound();
public void sleep() {
System.out.println("Sleeping...");
}
}
class Dog extends Animal {
@Override
public void makeSound() {
System.out.println("Bark");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.makeSound();
dog.sleep();
}
}
多态允许同一个接口在不同的实现中表现出不同的行为。它是通过方法重载和方法重写实现的。
class Animal {
public void makeSound() {
System.out.println("Some sound");
}
}
class Dog extends Animal {
@Override
public void makeSound() {
System.out.println("Bark");
}
}
class Cat extends Animal {
@Override
public void makeSound() {
System.out.println("Meow");
}
}
public class Main {
public static void main(String[] args) {
Animal myDog = new Dog();
Animal myCat = new Cat();
myDog.makeSound();
myCat.makeSound();
}
}
Java 的 I/O 库提供了丰富的类和接口,用于文件操作、数据流操作、网络通信等。
Scanner 类,用于从各种输入源读取数据,例如控制台输入、文件、字符串等。它提供了一系列方便的方法来解析基本类型和字符串。
import java.util.Scanner;
public class ScannerExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入您的名字: ");
String name = scanner.nextLine(); // 读取整行输入
System.out.println("你好, " + name + "!");
System.out.print("请输入您的年龄: ");
int age = scanner.nextInt(); // 读取整数输入
System.out.println("您 " + age + " 岁了!");
scanner.close(); // 关闭 Scanner
}
}
上述代码从控制台获取
常用方法:
nextLine(): 读取一整行输入,返回一个字符串。
nextLine().charAt(0): 读取一行字符串中的第一个,一般用这个读取 char 类型变量。
nextInt(): 读取一个整数。
nextDouble(): 读取一个双精度浮点数。
nextBoolean(): 读取一个布尔值。
hasNext(): 检查是否有下一个输入。
hasNextLine(): 检查是否有下一行输入。
close(): 关闭 Scanner。
BufferedReader 类用于从字符输入流中读取文本,提供了缓冲功能以提高读取效率。它通常与
InputStreamReader 一起使用,从标准输入或文件读取数据。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class BufferedReaderExample {
public static void main(String[] args)throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("请输入您的名字: ");
String name = reader.readLine(); // 读取整行输入
System.out.println("你好, " + name + "!");
System.out.print("请输入您的年龄: ");
int age = Integer.parseInt(reader.readLine()); // 读取整行输入并解析为整数
System.out.println("您 " + age + " 岁了!");
reader.close(); // 关闭 BufferedReader
}
}
上述代码从控制台读取数据
常用方法:
readLine(): 读取一整行输入,返回一个字符串。 close(): 关闭 BufferedReader。对比 Scanner 和 BufferedReader
都可以用于从终端、文件等源读取输入。
都提供了读取整行输入的方法:nextLine() 和 readLine()。
Scanner 提供了更多解析输入数据的方法,如 nextInt(),nextDouble() 等,可以直接读取基本类型数据。
BufferedReader 主要用于读取字符串,需要手动解析基本类型数据。
BufferedReader 通常性能更高,因为它使用缓冲机制,适合读取大量文本数据。
Scanner 在方便性上有优势,但性能可能稍逊色。
Scanner 更适合处理交互式的终端输入,或者需要解析各种基本类型数据的场景。
BufferedReader 更适合读取大量文本数据,或者需要更高效的输入操作的场景。
在 Java 中,try-catch语句用于处理异常。异常是程序运行过程中发生的错误或不预期的情况,try-catch块允许你捕获和处理这些异常,以避免程序崩溃并提供更好的用户体验。
public class FinallyExample {
public static void main(String[] args) {
try {
System.out.println("尝试打开文件");
// 模拟文件操作
} catch(Exception e) {
System.out.println("处理异常: " + e.getMessage());
} finally {
System.out.println("无论如何,都会执行的代码");
}
}
}
finally块用于执行清理代码,无论是否发生异常,它都会执行。通常用于关闭文件、释放资源等操作。
重新抛出异常
在catch块中,你可以选择重新抛出异常,以便在更高的层次处理它:****
public class RethrowExceptionExample {
public static void main(String[] args) {
try {
methodThatThrowsException();
} catch(Exception e) {
System.out.println("捕获异常: " + e.getMessage());
throw e; // 重新抛出异常
}
}
public static void methodThatThrowsException()throws Exception {
throw new Exception("这是一个异常");
}
}
在这个示例中,异常在 catch块中被捕获并重新抛出,允许调用方法的代码进一步处理异常。
try-with-resources语句(通常被称为 try 语句)是 Java 7 引入的一个特性,它简化了资源管理,特别是用于自动关闭实现了AutoCloseable接口的资源,如InputStream、OutputStream、Connection等。try-with-resources语句保证在 try 块执行完毕后,即使发生异常,资源也会被自动关闭。
try-with-resources语句在try块执行完毕后,会自动调用每个资源的 close()方法。即使在 try块中发生了异常,资源的 close()方法也会被调用。因此,不需要在 finally块中显式地关闭资源,这样可以减少代码重复并提高代码的可读性。
语法:
try(ResourceType resource = new ResourceType()) {
// 使用资源的代码
} catch(ExceptionType e) {
// 异常处理代码
}
**ResourceType**: 实现了 AutoCloseable或 java.io.Closeable接口的资源类型。**resource**: 在 try块中使用的资源对象。**try**** **块: 包含使用资源的代码。**catch**** 块**: 处理在 try块中可能发生的异常。import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class TryWithResourcesExample {
public static void main(String[] args) {
try(BufferedReader reader = new BufferedReader(new FileReader("file.txt"))) {
String line;
while((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch(IOException e) {
System.out.println("读取文件时发生错误: " + e.getMessage());
}
}
}
BufferedReader实现了AutoCloseable接口,因此可以被用于try-with-resources语句中,在这个示例中,BufferedReader会在try块执行完成后自动关闭,无需显式调用 close()方法。
可以在 try-with-resources语句中管理多个资源,这些资源会按照声明的顺序关闭
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
public class MultipleResourcesExample {
public static void main(String[] args) {
try(BufferedReader reader = new BufferedReader(new FileReader("file.txt"));
GZIPInputStream gzipInputStream = new GZIPInputStream(new FileInputStream("file.txt.gz"))) {
// 使用 reader 和 gzipInputStream 进行操作
} catch(IOException e) {
System.out.println("发生了 IO 异常: " + e.getMessage());
}
}
}
在这个示例中,BufferedReader和GZIPInputStream会在try块执行完成后自动关闭。
如果你定义了一个自定义类,并希望它在try-with-resources中使用,那么这个类需要实现AutoCloseable接口(或者java.io.Closeable接口)
public class CustomResource implements AutoCloseable {
@Override
public void close() {
System.out.println("CustomResource closed");
}
public void doSomething() {
System.out.println("Doing something with CustomResource");
}
public static void main(String[] args) {
try(CustomResource resource = new CustomResource()) {
resource.doSomething();
} catch(Exception e) {
System.out.println("发生了异常: " + e.getMessage());
}
}
}
在这个示例中,CustomResource实现了AutoCloseable接口,因此可以在try-with-resources中使用,并在 try块结束后自动关闭。

Java 集合框架主要包括两种类型的容器,一种是集合(Collection),存储一个元素集合,另一种是图(Map),存储键/值对映射。Collection 接口又有 3 种子类型,List、Set 和 Queue,再下面是一些抽象类,最后是具体实现类,常用的有 ArrayList、LinkedList、HashSet、LinkedHashSet、HashMap、LinkedHashMap 等等。
集合框架是一个用来代表和操纵集合的统一架构。所有的集合框架都包含如下内容:
除了集合,该框架也定义了几个 Map 接口和类。Map 里存储的是键/值对。尽管 Map 不是集合,但是它们完全整合在集合中。
