Simple, practical, book-style explanation of the fundamental java.lang package.
The source chapter describes java.lang as Java's most widely used package. It contains classes and interfaces fundamental to virtually all Java programming. Beginning with JDK 9, java.lang is part of the java.base module.
Java has primitive types such as int, char, and double. Sometimes a primitive value needs an object representation—for example, collection classes work with objects. Java therefore provides wrapper classes.
The chapter notes that wrapper classes are value-based beginning with JDK 16 and should not be used for synchronization.
Number is an abstract superclass for numeric wrapper classes such as Byte, Short, Integer, Long, Float, and Double.
| Method | Purpose |
|---|---|
| byteValue() | Returns the value as byte. |
| shortValue() | Returns the value as short. |
| intValue() | Returns the value as int. |
| longValue() | Returns the value as long. |
| floatValue() | Returns the value as float. |
| doubleValue() | Returns the value as double. |
Integer and Long provide parsing, conversion, comparison, and radix-related utilities.
Common radix values are 2 for binary, 8 for octal, 10 for decimal, and 16 for hexadecimal.
Output for the example:
Byte and Short wrap the corresponding primitive types. Like Integer and Long, they provide conversion, parsing, comparison, and unsigned-related utilities where applicable.
Float and Double wrap floating-point values.
Typical output:
The classes provide methods for detecting special floating-point values, converting values, comparing values, and parsing strings.
Boolean wraps a boolean value and provides conversion, comparison, and logical helper methods.
| Method | Purpose |
|---|---|
| parseBoolean(String) | Converts "true" into true; case is not significant. |
| valueOf(boolean) | Returns Boolean representing the value. |
| booleanValue() | Gets the primitive boolean. |
| logicalAnd() | Logical AND. |
| logicalOr() | Logical OR. |
| logicalXor() | Logical XOR. |
Character wraps a char. It also provides many methods for testing and converting characters.
Useful methods include isDigit(), isLetter(), isWhitespace(), isUpperCase(), isLowerCase(), toUpperCase(), and toLowerCase().
The chapter's earlier String discussion is extended by the mutable string classes.
| Class | Main idea |
|---|---|
| String | Immutable character sequence. |
| StringBuffer | Mutable character sequence with synchronization. |
| StringBuilder | Mutable character sequence without synchronization; generally faster when external synchronization is unnecessary. |
Object is the root of Java's class hierarchy. Classes that do not explicitly extend another class ultimately inherit from Object.
| Method | Purpose |
|---|---|
| clone() | Creates a copy of the object when cloning is supported. |
| equals(Object) | Tests logical/object equality according to the implementation. |
| getClass() | Returns a Class object describing the object. |
| hashCode() | Returns the object's hash code. |
| toString() | Returns a string representation. |
| wait() | Coordinates waiting between threads. |
| notify() | Notifies one waiting thread. |
| notifyAll() | Notifies waiting threads. |
The source explains that cloning creates a new object that is initially an exact copy. Reference fields are copied as references, so the clone can still refer to the same underlying objects. This can cause unintended side effects.
Class represents classes and interfaces at run time. It lets a program discover information about a type while the program is running.
| Method | Purpose |
|---|---|
| getName() | Gets the complete class/interface name. |
| getPackageName() | Gets the package name. |
| getSuperclass() | Gets the superclass. |
| getMethods() | Gets public methods. |
| getConstructors() | Gets public constructors. |
| getFields() | Gets public fields. |
| isInterface() | Tests whether the type is an interface. |
| getModule() | Gets the module containing the class. |
The source explains that run-time type information is important for reflection and Java Beans. Reflection can inspect constructors, fields, methods, and modifiers dynamically.
System contains important static facilities. Standard input, standard output, and standard error are available through System.in, System.out, and System.err.
| Method | Purpose |
|---|---|
| arraycopy() | Copies elements between arrays. |
| currentTimeMillis() | Current time in milliseconds since January 1, 1970. |
| getenv() | Gets environment variables. |
| getProperty() | Gets a Java system property. |
| gc() | Requests garbage collection. |
| exit(int) | Terminates the JVM with an exit code. |
| console() | Returns the JVM console when available. |
| lineSeparator() | Returns the platform line separator. |
| getLogger() | Gets a system logger. |
System properties provide information about the Java run-time environment and operating system.
Runtime represents the run-time environment associated with the Java application. Obtain it using Runtime.getRuntime().
| Method | Purpose |
|---|---|
| getRuntime() | Returns the current Runtime object. |
| totalMemory() | Approximate total memory available to the program. |
| freeMemory() | Approximate free memory available. |
| gc() | Initiates garbage collection. |
| exec() | Starts another operating-system process. |
| addShutdownHook() | Registers a thread to run during JVM termination. |
| removeShutdownHook() | Removes a shutdown hook. |
| version() | Returns Java runtime version information. |
Java can execute other programs through Runtime's exec(). It returns a Process object that can be used to interact with the subprocess.
The chapter describes destroy(), waitFor(), and exitValue(), plus streams/readers used to communicate with a subprocess.
ProcessBuilder provides another way to create and configure operating-system processes. It accepts the program name and command-line arguments.
| Method | Purpose |
|---|---|
| command() | Gets or sets the program and arguments. |
| directory() | Gets or sets the working directory. |
| environment() | Gets environment variables. |
| inheritIO() | Uses the same standard I/O as the current process. |
| redirectInput() | Redirects standard input. |
| redirectOutput() | Redirects standard output. |
| redirectError() | Redirects standard error. |
| redirectErrorStream() | Merges standard error into standard output. |
| start() | Starts the configured process. |
Math provides mathematical functions and constants.
| Category | Examples |
|---|---|
| Absolute value | abs() |
| Maximum/minimum | max(), min() |
| Power/root | pow(), sqrt(), cbrt() |
| Trigonometry | sin(), cos(), tan() |
| Logarithms | log(), log10() |
| Rounding | ceil(), floor(), round() |
| Random | random() |
| Angles | toRadians(), toDegrees() |
StrictMath provides a mathematical API parallel to Math. The source explains that StrictMath historically emphasized precisely identical results across Java implementations, while Math allowed more implementation latitude for performance. It also notes that beginning with JDK 17, all math computations are strict.
Runnable defines the entry point for a separate thread of execution. It has one abstract method: run().
Thread creates and controls a thread of execution. It implements Runnable.
| Method | Purpose |
|---|---|
| start() | Starts thread execution. |
| run() | Contains the thread's work. |
| currentThread() | Returns the current Thread. |
| getName() | Gets the thread name. |
| getPriority() | Gets priority. |
| setPriority() | Sets priority. |
| getState() | Gets thread state. |
| getStackTrace() | Gets stack trace information. |
ThreadGroup groups threads together. A newly created thread normally belongs to the same thread group as its parent unless another group is specified.
ThreadLocal provides thread-local storage: each thread can have its own independent value associated with the same ThreadLocal variable.
ProcessHandle provides information and control over operating-system processes. The source discusses process identification and the ability to determine whether a process is still alive.
ProcessHandle.Info provides process information such as command and CPU-duration information.
Package represents information about a Java package.
| Method | Purpose |
|---|---|
| getName() | Gets package name. |
| getImplementationTitle() | Gets implementation title. |
| getImplementationVendor() | Gets implementation vendor. |
| getImplementationVersion() | Gets implementation version. |
| getSpecificationTitle() | Gets specification title. |
| getSpecificationVendor() | Gets specification vendor. |
| getSpecificationVersion() | Gets specification version. |
| isSealed() | Tests whether the package is sealed. |
Added by JDK 9, Module encapsulates a Java module. It can provide information about module access and packages and can participate in module relationships.
| Method | Purpose |
|---|---|
| getName() | Gets module name. |
| getPackages() | Gets packages in the module. |
| getDescriptor() | Gets module descriptor information. |
| isNamed() | Checks whether module is named. |
| isExported() | Checks whether a package is exported. |
| isOpen() | Checks whether a package is open. |
| canRead() | Checks module readability. |
| canUse() | Checks service usage. |
ModuleLayer, added by JDK 9, represents a module layer. Its nested Controller class controls a module layer. The source describes these as specialized facilities.
StackTraceElement describes one frame in a stack trace. A frame identifies an execution point and can include class name, method name, source file, source line number, and module information.
Normally, you obtain StackTraceElement objects through methods such as Throwable.getStackTrace() or Thread.getStackTrace().
Added by JDK 16, Record is the superclass for all Java records. Records automatically inherit from Record, and Record provides/overrides object-level behavior such as equals(), hashCode(), and toString().
ClassValue<T> associates a value with a type. The source describes it as a specialized facility rather than something normally needed in everyday programming.
CharSequence defines read-only access to a sequence of characters. It is implemented by types including String, StringBuffer, and StringBuilder.
| Method | Purpose |
|---|---|
| charAt(int) | Returns a character at an index. |
| compare(CharSequence, CharSequence) | Compares two character sequences. |
| chars() | Returns an IntStream of characters. |
| codePoints() | Returns an IntStream of code points. |
| isEmpty() | Tests whether the sequence has no characters. |
| length() | Returns the number of characters. |
| subSequence() | Returns a subsequence. |
| toString() | Returns a String representation. |
Runtime.Version encapsulates version information for the Java environment. The source notes that it was added by JDK 9 and changed substantially with JDK 10 to accommodate the newer release cadence.
RuntimePermission relates to Java's security mechanism. The source also notes that SecurityManager has been deprecated for removal beginning with JDK 17.
Compiler supports environments where Java bytecode can be compiled into executable code rather than interpreted. The source describes it as not intended for normal programming use and deprecated for removal.
| Requirement | Useful type |
|---|---|
| Object's common behavior | Object |
| Inspect a type at runtime | Class |
| Wrap a primitive | Integer, Double, Boolean, Character, etc. |
| Parse integer text | Integer / Long |
| Character classification | Character |
| Mutable text | StringBuffer / StringBuilder |
| System properties/environment | System |
| JVM runtime information | Runtime |
| Start an external process | Runtime / ProcessBuilder |
| Mathematical operations | Math / StrictMath |
| Create a thread | Thread / Runnable |
| Per-thread value | ThreadLocal |
| Process information | ProcessHandle |
| Package information | Package |
| Module information | Module |
| Stack frame information | StackTraceElement |
| Read-only character abstraction | CharSequence |
The following tree gives a quick visual view of the major inheritance relationships discussed in this chapter. Remember that Boolean and Character are wrapper classes but do not extend Number.
Object
│
├── Number
│ │
│ ├── Byte
│ ├── Short
│ ├── Integer
│ ├── Long
│ ├── Float
│ └── Double
│
├── Boolean
│
├── Character
│
├── String
│
├── StringBuffer
│
├── StringBuilder
│
├── Thread
│
└── Throwable
│
├── Exception
│ ├── RuntimeException
│ └── ...
│
└── Error
├── ThreadDeath
└── ...Object → Number → Byte, Short, Integer, Long, Float, Double. Boolean and Character directly extend Object.This hierarchy gives you a quick picture of the important relationships between the java.lang classes discussed in this chapter.