Java Standard Library: Complete Overview and Code Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Java Standard Library (Java Standard Edition): Full Overview



The Java Standard Library (Java Standard Edition — JavaSE) is a fundamental set of packages, classes, and interfaces that comes with every JDK (Java Development Kit). Unlike third-party frameworks, the standard library does not require installing additional dependencies — it is available "out of the box." It is what makes Java such a powerful and portable language, providing the developer with ready-made solutions for most everyday tasks: from working with strings to multithreading and network interaction.



In this overview, we will break down the key packages of the standard library, show practical code examples, and explain how these tools are used in real-world development.



1. Package java.lang — The Absolute Foundation



The java.lang package is imported automatically in any Java program. It contains classes without which no application can be imagined.



  • Object — the root class for all objects. Methods equals(), hashCode(), toString().
  • String, StringBuilder, StringBuffer — working with immutable and mutable strings.
  • Math — mathematical functions (Math.sqrt(), Math.random()).
  • System — console input/output, managing system properties.
  • Thread and Runnable — basic multithreading tools.


public class LangExample {    public static void main(String[] args) {        // Working with strings        String text = "Hello, Java!";        System.out.println(text.length()); // 13

// Math double sqrt = Math.sqrt(144); System.out.println(sqrt); // 12.0

// Object class Object obj = new Object(); System.out.println(obj.hashCode()); }}


2. Package java.util — Collections and Utilities



This is the most extensive package. It includes the Collections Framework, working with dates, random numbers, and much more.



Collections Framework

  • List (ArrayList, LinkedList) — ordered lists with duplicates.
  • Set (HashSet, TreeSet) — sets without repetitions.
  • Map (HashMap, TreeMap) — key-value dictionaries.
  • Queue (PriorityQueue) — queues.


import java.util.*;

public class UtilExample { public static void main(String[] args) { // ArrayList List<String> list = new ArrayList<>(); list.add("Java"); list.add("Python"); list.add("Java"); System.out.println(list); // [Java, Python, Java]

// HashSet (unique elements) Set<String> set = new HashSet<>(list); System.out.println(set); // [Java, Python]

// HashMap Map<String, Integer> map = new HashMap<>(); map.put("Apple", 10); map.put("Pear", 5); System.out.println(map.get("Apple")); // 10 }}


Utilities

  • Collections — static methods for sorting, searching, synchronization.
  • Arrays — working with arrays.
  • Random — generating pseudo-random numbers.


3. Package java.io — Classic Input/Output



Package for working with data streams. Used for reading and writing files, working with the console, and network streams.



  • InputStream/OutputStream — byte streams.
  • Reader/Writer — character streams.
  • File

Recommendations