Table of contents
1.
Introduction
2.
Interfaces
2.1.
Example:
3.
Classes
3.1.
Example:
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024

The java.lang Package

Author Yashesvinee V
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The java.lang package is one of Java's most fundamentally used packages. It contains classes that practically design most of your code. The java.lang package provides classes for Mathematical operations, String operations, and System operations. It also provides Wrapper classes that wrap data into objects.

One can find the java.lang package in the lib directory under the Java Runtime Environment installed folder. Java.lang is imported by default into a Java program by the compiler as it contains a number of components that give functionality to a program.

Also see, Duck Number in Java and Hashcode Method in Java.

Interfaces

Example:

The below example shows the use of the Runnable and CharSequence interface from the java.lang package

class ThreadDemo implements Runnable 
{
  public void run()
  { 
      System.out.println("Thread is running");
  }
}

 class StringSeq
{
      public void stringSequence(CharSequence chs)
      {
      System.out.println(chs.subSequence(6,10));
      }
}
 
public class Main 
{
  public static void main(String[] args)
  {
      StringSeq s = new StringSeq();
      s.stringSequence("Let's play");
      Thread object = new Thread(new ThreadDemo());
      object.start();
  }
} 
You can also try this code with Online Java Compiler
Run Code

 

Output:

Play
Thread is running

Classes

Example:

The below example shows the use of Math and String classes in java.lang.

public class Main 
{
  public static void main(String[] args)
  {
      String str = "Hello";
      System.out.println(str + " World"); 
      int a = 16, b = 2;
      System.out.println("Square root of 16 is " + Math.sqrt(a));
      System.out.println("Square of 16 is " + Math.pow(a,b));
  }
} 
You can also try this code with Online Java Compiler
Run Code

 

Output:

Hello World
Square root of 16: 4.0
Square of 16: 256.0

Also see, Swap Function in Java

FAQs

  1. What is the difference between String and CharSequence in java.lang?
    CharSequence is an interface that represents a sequence of characters, while String is a class used to create immutable strings. Mutable and immutable classes can implement CharSequence. 
    CharSequence does not use any comparison strategy, but the String class uses the Comparable interface to compare its instances.
     
  2. Why do we need to wrap primitive data type values in Java?
    Variables of the primitive data types are passed by value to other methods. In order to modify the arguments passed, they have to be converted into objects.
    Packages like java.util and Collection frameworks like Vectors can handle only objects and not values.

Key Takeaways

This article extensively discusses the classes and interfaces of the Java.lang package. We hope that this blog has helped you enhance your knowledge regarding this package and if you would like to learn more, check out our articles on the Coding Ninjas Library. Do upvote our blog to help other ninjas grow. Happy Coding!

Related Links:

Live masterclass