Understanding Java.Lang Package: Classes, Methods & Usage
The java.lang package is one of the most fundamental packages in Java, providing essential classes required for writing any Java program. It is automatically imported in every Java program, so programmers do not need an import statement to use its classes.
Overview of Java.Lang Package
- Known as the basic package in Java.
- Contains the Object class, which is the parent of every Java class.
- Provides String, StringBuilder, StringBuffer, and wrapper classes.
- Supports both final and non-final classes:
- Final classes: String, StringBuilder, StringBuffer, wrapper classes. (Cannot be inherited)
- Non-final classes: Object, Number.
Key Classes in Java.Lang
1. Object Class
The Object class is the root of all Java classes. Some important methods include:
- toString(): Returns object information as a string in the format packageName.className@hexAddress.
- hashCode(): Converts the hexadecimal memory address to an integer memory address.
- equals(Object obj): Compares two objects based on memory address. Returns true if they are the same.
- clone(): Creates an exact copy of an object in heap memory. Requires CloneNotSupportedException handling.
- finalize(): Performs cleanup via garbage collection before the object is removed from memory.
- wait(), notify(), notifyAll(): Methods used for synchronization in multithreading.
2. Marker Interface
A marker interface is an empty interface with no methods or variables.
- Indicates special behavior to JVM.
- Examples:
Cloneable: Enables object cloning.
Serializable: Enables object serialization.
3. Object Cloning
- Process of creating an exact copy of an object.
- Requirements:
Class must implement the Cloneable interface.
Use the clone() method from the Object class.
4. Wrapper Classes
Wrapper classes provide object representation for primitive data types.
- All wrapper classes are final.
- Supports:
Boxing: Convert primitive → object.
Unboxing: Convert object → primitive.
Parsing: Convert string → primitive (except Character).
- Examples: Integer, Double, Boolean, Character, etc.
5. String Class
- Immutable class used to store text.
- Can be created using:
String literals: Stored in String Constant Pool.
JVM reuses existing strings if present.
new keyword: Always creates a new object in heap memory.
length(), charAt(int index), contains(String s)
startsWith(String s), endsWith(String s), concat(String s)
substring(int start, int end), toUpperCase(), toLowerCase()
toCharArray(), split(String regex)
- Immutable Nature: Any modification creates a new string object.
6. StringBuilder Class
- Mutable string class for dynamic modifications.
- Common methods:
insert(), delete(), append(), reverse()
7. StringBuffer Class
- Similar to StringBuilder but thread-safe (synchronized).
- Used in multithreaded applications for string modifications.
Conclusion
The java.lang package is the backbone of Java programming. It provides essential classes like Object, String, StringBuilder, and wrapper classes that simplify development. Understanding its classes and methods is crucial for writing robust and efficient Java programs.
Comments
Post a Comment