1. Object
Class
- Package:
java.lang
- Role: The root class of all classes in Java. Every class inherits from
Object
. - Key Methods:
-
toString()
Returns a string representation of the object. equals(Object obj)
Checks if two objects are equal.hashCode()
Returns a hash code for the object.getClass()
Returns the runtimeClass
object of the object.clone()
Creates and returns a copy of the object.notify()
,notifyAll()
,wait()
Used for thread communication.
Example:
class Person {
String name;
Person(String name) { this.name = name; }
@Override
public String toString() { return "Person[name=" + name + "]"; }
}
public class Main {
public static void main(String[] args) {
Person p = new Person("Alice");
System.out.println(p); // Person[name=Alice]
}
}
2. Class
Class
- Package:
java.lang
- Role: Represents the metadata (runtime type information) of a class or interface.
- How to Obtain a
Class
Object: -
String s = "Hello"; Class<?> c1 = s.getClass(); // via object
Class<?> c2 = String.class; // via class literal
Class<?> c3 = Class.forName("java.lang.String"); // via name
- Key Methods:
-
getName()
Returns class name. getSuperclass()
Returns superclass.getInterfaces()
Returns implemented interfaces.-
getMethods()
Returns public methods. isInterface()
Checks if it’s an interface.isArray()
Checks if it’s an array type.
Example:
public class Main {
public static void main(String[] args) throws Exception {
Class<?> cls = Class.forName("java.lang.String");
System.out.println("Class Name: " + cls.getName());
System.out.println("Superclass: " + cls.getSuperclass().getName());
}
}
3. Math
Class
- Package:
java.lang
- Role: Provides static methods for mathematical operations.
- Key Methods:
Math.abs(x)
Absolute value.Math.sqrt(x)
Square root.Math.pow(a,b)
Power calculation.Math.max(a,b)
/Math.min(a,b)
Max/min values.Math.random()
Random double (0.0–1.0).Math.round(x)
Rounds value to nearest integer.
Example:
public class Main {
public static void main(String[] args) {
System.out.println(Math.sqrt(16)); // 4.0
System.out.println(Math.pow(2, 3)); // 8.0
System.out.println(Math.random()); // e.g., 0.734
}
}
4. System
Class
- Package:
java.lang
- Role: Provides system-related utility methods and fields.
- Important Fields:
System.in
→ Standard input stream.System.out
→ Standard output stream.System.err
→ Standard error stream.
- Key Methods: Method Purpose
System.currentTimeMillis()
Current time in milliseconds.System.arraycopy()
Copies array elements.System.exit(status)
Terminates the JVM.System.gc()
Suggests garbage collection.System.getProperty(key)
Gets system property.
Example:
public class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
long time = System.currentTimeMillis();
System.out.println("Time in ms: " + time);
}
}
Quick Summary Table
Class | Package | Purpose |
---|---|---|
Object | java.lang | Base class for all Java objects. |
Class | java.lang | Represents runtime type information. |
Math | java.lang | Provides mathematical utility methods. |
System | java.lang | Provides system-level operations. |