1. java.lang 包
java.lang 包是自动导入的,包含了Java编程的基础类,如 String, Math, System, Thread 等。
字符串示例
public class StringExample {
public static void main(String[] args) {
String greeting = "Hello, World!";
System.out.println(greeting.toUpperCase()); // 输出: HELLO, WORLD!
System.out.println(greeting.toLowerCase()); // 输出: hello, world!
System.out.println(greeting.contains("World")); // 输出: true
}
}
2. java.util 包
java.util 包提供了包含集合框架、日期时间工具、实用工具类等。
集合框架示例
import java.util.ArrayList;
import java.util.List;
public class CollectionExample {
public static void main(String[] args) {
List<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
for (String fruit : fruits) {
System.out.println(fruit);
}
}
}
3. java.io 包
java.io 包提供了文件输入/输出操作的类。
文件读写示例
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class FileExample {
public static void main(String[] args) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter("example.txt"))) {
writer.write("Hello, File!");
} catch (IOException e) {
e.printStackTrace();
}
try (BufferedReader reader = new BufferedReader(new FileReader("example.txt"))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
4. java.net 包
java.net 包提供了用于网络通信的类。
网络请求示例
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class NetworkExample {
public static void main(String[] args) {
try {
URL url = new URL("http://www.example.com");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
5. java.time 包
java.time 包是Java 8引入的新日期和时间API。
日期时间示例
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();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedNow = now.format(formatter);
System.out.println("Current time: " + formattedNow);
}
}
掌握这些核心Java库是成为一名熟练Java开发者的基础。
看到这些可能还是懵逼的一片,这些有个印象我觉得就行,等后面真正的开始理解了,再去深剖析这些。

被折叠的 条评论
为什么被折叠?



