The java.lang package is one of the most fundamental packages in Java. This package provides essential functionality such as object manipulation, string handling, mathematical operations, exception handling, threading, and system-level operations.
- Automatically imported by the Java compiler.
- Contains core classes required by every Java program.
- Provides support for strings, wrappers, threads, exceptions, and system operations.
Why is the java.lang Package Important?
- It is automatically available in every Java program.
- It contains the most commonly used classes in Java.
- It provides the Object class, which is the parent of all Java classes.
- It offers classes for working with strings, such as String and StringBuilder.
- It provides mathematical functions through the Math class.
- It supports exception handling using classes like Exception and Throwable.
Common Classes in java.lang
| Class | Description |
|---|---|
Object | Root superclass of all Java classes. |
String | Represents an immutable sequence of characters. |
| StringBuilder | Mutable sequence of characters for efficient string manipulation. |
| StringBuffer | Thread-safe mutable sequence of characters. |
System | Provides access to system resources, standard input/output, and environment variables. |
Math | Contains methods for performing mathematical operations. |
Thread | Represents a thread of execution in a Java program. |
Runnable | Interface used to define tasks executed by threads. |
Exception | Base class for exceptions that can be handled by applications. |
Error | Represents serious system-level errors that applications typically do not handle. |
Throwable | Superclass of both Exception and Error. |
Runtime | Provides access to the Java runtime environment. |
Process | Represents a process running in the operating system. |
Class | Represents metadata about classes and interfaces at runtime. |
Integer | Wrapper class for the primitive int type. |
Long | Wrapper class for the primitive long type. |
Double | Wrapper class for the primitive double type. |
Float | Wrapper class for the primitive float type. |
Character | Wrapper class for the primitive char type. |
Boolean | Wrapper class for the primitive boolean type. |
Byte | Wrapper class for the primitive byte type. |
Short | Wrapper class for the primitive short type. |
Void | Represents the void return type. |
Enum | Base class for all Java enumerations. |
Package | Contains information about Java packages. |
Advantages of java.lang Package
- Automatically Imported: No need to explicitly import the package in Java programs.
- Provides Core Java Classes: Contains fundamental classes required for almost every Java application.
- Supports Object-Oriented Programming: Includes the
Objectclass, the root of the Java class hierarchy. - Simplifies String Handling: Offers classes like
String,StringBuilder, andStringBuffer. - Built-in Mathematical Functions: The
Mathclass provides common mathematical operations. - Exception Handling Support: Contains classes for handling exceptions and errors.
- Multithreading Capabilities: Provides the
ThreadandRunnableclasses. - Wrapper Class Support: Includes wrapper classes such as
Integer,Double, andBoolean. - System-Level Operations: The
SystemandRuntimeclasses allow interaction with the JVM and operating system.