Java Standard Library: A Complete Overview
The Java Standard Library is a fundamental set of packages, classes, and interfaces that comes with every JDK (Java Development Kit) installation. It provides developers with everything needed to create applications: from working with basic data types and collections to network communication and multithreading. Understanding this library is key to effective Java development.
Main Packages of the Standard Library
The library is organized into packages. Let's look at the most important ones:
java.lang— an automatically imported package containing wrapper classes (Integer,Double), strings (String,StringBuilder), system functions (System,Math), and exceptions.java.util— collections, date and time handling, random numbers, utilities (Arrays,Collections).java.io— input/output streams, file handling, and serialization.java.nio— non-blocking I/O (New I/O), buffers, and channels.java.net— network programming (sockets, URL, HTTP).java.util.concurrent— multithreading: executors, blocking queues, atomic variables.java.time(Java 8+) — modern API for date and time handling (LocalDate,ZonedDateTime).
The java.lang Package: The Foundation of Everything
The java.lang package does not require explicit import. It includes the Object class — the root of the entire class hierarchy. Let's look at the key elements:
// String class — immutable stringsString greeting = "Hello, Java!";String upper = greeting.toUpperCase(); // "HELLO, JAVA!"
// StringBuilder — mutable strings (more efficient than concatenation in a loop)StringBuilder sb = new StringBuilder("Hello");sb.append(" World");sb.insert(5, ",");System.out.println(sb.toString()); // "Hello, World"
// Math class — mathematical operationsdouble pi = Math.PI;double sqrt = Math.sqrt(144); // 12.0int max = Math.max(10, 20); // 20Special attention should be paid to wrapper classes (Integer, Boolean, etc.). They allow primitives to be used in collections and provide conversion methods:
int primitive = 42;Integer wrapped = Integer.valueOf(primitive); // Autoboxingint back = wrapped.intValue(); // Unboxing
// Parsing a string into a numberint parsed = Integer.parseInt("12345");System.out.println(parsed); // 12345The java.util Package: Collections and Utilities
This is the most extensive package. Its core is the Collections Framework:
- List — ordered lists:
ArrayList(fast index-based access),LinkedList(fast insertion/deletion). - Set — sets without duplicates:
HashSet,TreeSet(sorted). - Map — key-value dictionaries:
HashMap,TreeMap.
import java.util.*;
// Example of working with HashMapMap<String, Integer> ages = new HashMap<>();ages.put("Alice", 30);ages.put("Bob", 25);ages.put("Charlie", 35);
for (Map.Entry<String, Integer> entry : ages.entrySet()) { System.out.println(entry.getKey() + ": " + entry.getValue());}
// Sorting a listList<Integer> numbers = new ArrayList<>(Arrays.asList(5, 3, 8, 1));Collections.sort(numbers);System.out.println(numbers); // [1, 3, 5, 8]Also in the package are