31 Java 常用基本库入门指南

31 Java 常用基本库入门指南

在Java中,基本库提供了许多重要的工具和功能,帮助开发者更高效地编写代码。本节将详细介绍一些常用的基本库和它们的重要组成部分。

1. java.lang

java.lang 包是Java的核心库之一,默认被导入到每个Java程序中。它包含了许多基础类。

1.1. String

String 类用于表示字符串。字符串是不可变的,即一旦被创建就不能更改。

示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class StringExample {
public static void main(String[] args) {
String greeting = "Hello, World!";

// 获取字符串长度
int length = greeting.length();
System.out.println("String Length: " + length);

// 字符串连接
String name = "Java";
String message = greeting + " Welcome to " + name + " programming.";
System.out.println(message);

// 字符串比较
boolean isEqual = greeting.equals("Hello, World!");
System.out.println("Strings are equal: " + isEqual);
}
}

1.2. Math

Math 类提供了基本的数学运算方法和常量。

示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class MathExample {
public static void main(String[] args) {
double radius = 5.0;

// 计算圆面积
double area = Math.PI * Math.pow(radius, 2);
System.out.println("Area of the circle: " + area);

// 取绝对值
int absoluteValue = Math.abs(-10);
System.out.println("Absolute Value: " + absoluteValue);

// 随机数生成
double randomValue = Math.random();
System.out.println("Random Value: " + randomValue);
}
}

2. java.util

java.util 包提供了多种数据结构、工具类和其他功能。

2.1. 集合框架 (Collection Framework)

Java的集合框架包括了许多常用的数据结构,如 ListSetMap

2.1.1. ArrayList

ArrayList 是一个可动态调整大小的数组实现。

示例代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import java.util.ArrayList;

public class ArrayListExample {
public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");

// 遍历 ArrayList
for (String fruit : fruits) {
System.out.println(fruit);
}

// 获取元素
String firstFruit = fruits.get(0);
System.out.println("First fruit: " + firstFruit);
}
}

2.1.2. HashMap

HashMap 是一种基于哈希表的键值对存储结构。

示例代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.HashMap;

public class HashMapExample {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
map.put("Alice", 30);
map.put("Bob", 25);

// 获取值
int ageOfAlice = map.get("Alice");
System.out.println("Age of Alice: " + ageOfAlice);

// 遍历 HashMap
for (String key : map.keySet()) {
System.out.println(key + ": " + map.get(key));
}
}
}

2.2. 日期和时间 (java.time 包)

自Java 8起,java.time 包提供了一组强大的工具类用于处理日期和时间。

示例代码(使用 LocalDateLocalDateTime):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class DateTimeExample {
public static void main(String[] args) {
// 获取当前日期
LocalDate today = LocalDate.now();
System.out.println("Today's date: " + today);

// 获取当前日期时间
LocalDateTime now = LocalDateTime.now();
System.out.println("Current date and time: " + now);

// 格式化日期时间
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
String formattedDate = now.format(formatter);
System.out.println("Formatted date and time: " + formattedDate);
}
}

3. 输入输出 (java.iojava.nio)

Java提供了丰富的输入输出操作类,用于处理文件、网络等。

3.1. 文件操作 (java.io)

使用 File 类进行文件操作,例如读写文件。

示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class FileExample {
public static void main(String[] args) {
File file = new File("example.txt");

try {
// 创建文件
if (file.createNewFile()) {
System.out.println("File created: " + file.getName());
} else {
System.out.println("File already exists.");
}

// 写入文件
FileWriter writer = new FileWriter(file);
writer.write("Hello, World!");
writer.close();
System.out.println("Successfully wrote to the file.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}

结论

本节介绍了Java的一些常用基本库,包括java.langjava.util以及输入输出库。这些库为Java开发提供了基础和支持,使得编写程序变得更加高效和便捷。希望这些示例和讲解能帮助你更好地理解和使用Java的基本库。

31 Java 常用基本库入门指南

https://zglg.work/java-zero/31/

作者

AI教程网

发布于

2024-08-08

更新于

2024-08-10

许可协议