java.lang Package

java.lang Package

The java.lang package is a special package in Java that is automatically imported into every Java program. It contains fundamental classes and interfaces that are essential for basic Java programming.

Class/Interface Description
Object The root class of all Java classes.
String A class for representing strings of characters.
Integer A class for representing integer values.
Double A class for representing double-precision floating-point values.
Boolean A class for representing boolean values.
Character A class for representing characters.
Math A class for mathematical operations and functions.
System A class providing access to system-related resources and functionalities.
Thread A class for creating and controlling threads of execution.

Example 1: String Class

The java.lang.String class is part of the java.lang package. It provides methods and functionalities to work with strings in Java.

import java.lang.String;

public class StringExample {
  public static void main(String[] args) {
    String message = "Hello, World!";
    int length = message.length();
    System.out.println("Length of the message: " + length);
    
    String upperCase = message.toUpperCase();
    System.out.println("Uppercase message: " + upperCase);
  }
}

Example 2: Math Class

The java.lang.Math class provides mathematical functions and constants in Java.

import java.lang.Math;

public class MathExample {
  public static void main(String[] args) {
    int x = 5;
    int y = 10;
    
    int max = Math.max(x, y);
    System.out.println("Maximum value: " + max);
    
    double sqrt = Math.sqrt(x);
    System.out.println("Square root of " + x + ": " + sqrt);
  }
}

In the above examples, we use classes from the java.lang package directly without explicit import statements because the java.lang package is automatically imported. We utilize the String class to perform operations on strings, such as getting the length and converting to uppercase. We also utilize the Math class to perform mathematical operations, such as finding the maximum value and calculating the square root.

The java.lang package provides essential classes and interfaces that are commonly used in Java programs. Since it is automatically imported, we can directly use classes from this package without importing them explicitly.

Go To Core Java Page Click Here

Post a Comment

Previous Post Next Post