Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
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
Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

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();
  }
} 

 

Output:

Play
Thread is running
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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));
  }
} 

 

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:

Previous article
java.util package
Next article
Difference Between Collection and Collections in Java
Live masterclass