W3docs

Java Built-in Packages

Tour of Java's most-used built-in packages — java.lang, java.util, java.io, java.nio, java.net, java.time.

The JDK ships with a sprawling standard library, organized into hundreds of packages. You'll spend the first few months reaching for the same handful over and over: java.lang, java.util, java.io, java.nio, java.net, java.time, java.math. This chapter is a tour — what each one is for, the headline classes inside it, and when to reach for it versus its neighbors.

java.lang — the package you never import

java.lang is imported automatically into every source file. Everything inside it is available by simple name without a single import line:

  • Object, Class, String, StringBuilder — the types every program touches.
  • Mathsqrt, min, max, random, the trig functions.
  • Wrapper typesInteger, Long, Double, Boolean, Character, Byte, Short.
  • Thread, Runnable, ThreadLocal — the original concurrency primitives.
  • Systemout, err, currentTimeMillis(), getenv, exit.
  • Throwable, Exception, RuntimeException, Error — the entire exception hierarchy.
  • Iterable, Comparable, CharSequence, AutoCloseable — interfaces that the language itself relies on for for-each, generics, try-with-resources, and friends.

If you see a class used without an import, it's almost always from here.

java.util — collections, utilities, and the old date classes

java.util is the daily-driver package. The collections framework alone is reason enough to know it.

  • Collection interfaces: Collection, List, Set, Map, Queue, Deque.
  • Concrete implementations: ArrayList, LinkedList, HashMap, LinkedHashMap, TreeMap, HashSet, TreeSet, ArrayDeque.
  • Utility classes: Arrays (array methods), Collections (sort, unmodifiableList, ...), Objects (requireNonNull, hash).
  • Optional — for return types that "might not have a value."
  • Random, Scanner, UUID — common utilities you'll see everywhere.
  • The old date classesDate, Calendar, TimeZone. Don't use these in new code; use java.time instead.

java.util also has important sub-packages worth knowing exist:

  • java.util.concurrentConcurrentHashMap, ExecutorService, Future, CompletableFuture, the modern concurrency toolbox.
  • java.util.stream — the Stream API for functional-style pipelines.
  • java.util.function — functional interfaces: Function, Predicate, Supplier, Consumer, the family used by streams and lambdas.

java.io — classic streams

The original I/O package, still in active use everywhere even though java.nio is now preferred for new code.

  • Byte streams: InputStream, OutputStream, FileInputStream, FileOutputStream, BufferedInputStream, ByteArrayInputStream.
  • Character streams: Reader, Writer, FileReader, FileWriter, BufferedReader, InputStreamReader, PrintWriter.
  • File — the legacy filesystem abstraction.
  • Serialization: Serializable, ObjectInputStream, ObjectOutputStream.

The split between byte streams (InputStream/OutputStream) and character streams (Reader/Writer) is intentional — bytes for binary data, characters for text with a known encoding.

java.nio — modern I/O and paths

"NIO" originally meant "new I/O"; nowadays it's just the recommended I/O package.

  • java.nio.file.Path — the modern replacement for java.io.File.
  • java.nio.file.Files — one-shot helpers: Files.readString, Files.write, Files.walk, Files.exists.
  • java.nio.file.Paths — older entry point; Path.of(...) does the same job in less code.
  • Buffers and channels: ByteBuffer, FileChannel, SocketChannel — lower-level building blocks for high-performance I/O.

For new code reading a file: reach for Files.readString(Path.of(...)), not BufferedReader plus a try-with-resources.

java.net — networking

URLs, sockets, and HTTP.

  • URL, URI — parsing and representing addresses.
  • Socket, ServerSocket, DatagramSocket — low-level TCP and UDP.
  • InetAddress — IPs and hostname resolution.

Java 11 added java.net.http.HttpClient, a modern, async-capable HTTP client built into the JDK. For new code making HTTP requests, that's the right starting point — no third-party library required.

java.time — dates and times done right

Added in Java 8 to replace the broken Date / Calendar API.

  • LocalDate, LocalTime, LocalDateTime — wall-clock values, no zone.
  • ZonedDateTime, ZoneId — zone-aware moments.
  • Instant — a point on the global timeline; the type to store in databases.
  • Duration, Period — amounts of time (seconds vs. days/months).
  • DateTimeFormatter — parsing and formatting.

All java.time types are immutable and thread-safe. That alone is enough reason to migrate any code still using java.util.Date.

java.math — precise arithmetic

When double and long aren't enough:

  • BigInteger — arbitrary-precision integers; no overflow.
  • BigDecimal — exact decimal arithmetic with an explicit rounding mode; what you want for money.

MathContext and RoundingMode round out the package.

A few others you'll meet

  • java.text — older formatting (NumberFormat, MessageFormat). Mostly superseded by java.time and String.format, but still common in localized output.
  • java.security — hashing, signing; MessageDigest, KeyStore, Signature.
  • javax.crypto — symmetric and asymmetric encryption.
  • java.sql — JDBC API.
  • javax.xml, org.w3c.dom, org.xml.sax — XML parsing.

The javax.* and org.* prefixes are historical — javax was originally for "extensions," and org.* packages came from standards bodies. Today they're regular JDK packages like the rest.

A worked example

This program pulls from java.util, java.util.stream, java.time, and java.math in one place — a snapshot of how the standard library composes.

java— editable, runs on the server

The same program would be twice as long and noisier with the pre-Java-8 Date/Calendar and double-for-money equivalents. Picking the right package is half the battle.

What's next

You've now seen what's in the standard library. The last piece of the packages story is how the JVM actually finds all those classes — yours and the JDK's — at compile time and runtime. Continue to the Java classpath.

Practice

Practice

A new Java project needs to handle prices and dates. Which packages should you reach for?