Java.lang Packages Tutorial

Java.lang package tutorial

Java - java.lang Package

Java.lang package contains the classes that are fundamental to the design of the Java programming language. Wrapper Classes like Integer, Boolean, Char etc are part of lang package. We're not supposed to import any class of java.lang package explicitly as being included by default. This tutorial cum reference will take you through all the methods available in java.lang package using simple and practical example.

Importing java.util Package

Being an inbuilt package of Java, we're not required to download any external library for java.lang package and its all classes can be imported using following syntax:

import java.lang.*;

Here we've used * operator to import all classes from java.lang package and now any class can be used in the program. In case of specific class, for example Thread, we can import a class using following syntax:

import java.lang.Thread;

Being a default package of java, import of java.lang package is not required and all classes at lang level are accessible without using import statements as well.

Why java.lang Package is used in Java Programs

Java.lang package classes contains fundamental classes like wrapper classes, classes for thread operations, classes for String manipulation, classes for mathematics operations etc. Following list shows some of the categories of classes of java.util package.

  • Wrapper Classes − java.lang package contains wrapper classes like Integer, Boolean, Float, Double etc for primitive values. These wrapper classes provides many useful methods and can be used as objects where primitive value is not to be used.

  • UI Components −java.lang.awt and java.lang.swing based components are core UI components of Java and provides event handling.

  • Mathematical operations − Math and StrictMath classes are rich with mathematical operations.

  • Thread operations −Thread, ThreadLocal, ThreadGroup etc. are important classes for multi-threading programs.

Important classes in java.lang Package

Following is the list of important classes in java.lang.package:

  • Boolean − The Boolean class wraps a value of the primitive type boolean in an object. An object of type Boolean contains a single field whose type is boolean.

  • Byte − The Byte class class wraps a value of primitive type byte in an object. An object of type Byte contains a single field whose type is byte.

  • Character − The Character class offers a number of useful class (i.e., static) methods for manipulating characters.

  • Character.Subset − The Character.Subset class instances represent particular subsets of the Unicode character set. The only family of subsets defined in the Character class is UnicodeBlock.

  • Character.UnicodeBlock − The Character.UnicodeBlock class is a family of character subsets representing the character blocks in the Unicode specification. Character blocks generally define characters used for a specific script or purpose.

  • Class − The Class class instance represent classes and interfaces in a running Java application. It has no public constructor.

  • ClassLoader − The ClassLoader class is an object that is responsible for loading classes. This class is an abstract class. It may be used by security managers to indicate security domains.

  • Compiler − The Compiler class is provided to support Java-to-native-code compilers and related services. By design, it serves as a placeholder for a JIT compiler implementation.

  • Double − The Double class wraps a value of primitive type double in an object. An object of type Double contains a single field whose type is double.

  • Enum − The Enum class is the common base class of all Java language enumeration types.

  • Float − The Float class wraps a value of primitive type float in an object. An object of type Float contains a single field whose type is float.

  • InheritableThreadLocal − The InheritableThreadLocal class extends ThreadLocal to provide inheritance of values from parent thread to child thread: when a child thread is created, the child receives initial values for all inheritable thread-local variables for which the parent has values.

  • Integer − The Integer class wraps a value of primitive type int in an object. An object of type Integer contains a single field whose type is int.

  • Long − The Long class wraps a value of the primitive type long in an object. An object of type Long contains a single field whose type is long.

  • Math − The Math class contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions.

  • Number − The Number class is an abstract class in java.lang package. It is the superclass of the classes that represent numeric values convertible to primitive data types such as byte, short, int, long, float, and double.

  • Object − The Object class is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class.

  • Package − The Package class contain version information about the implementation and specification of a Java package.

  • Process − The Process class provides methods for performing input from the process, performing output to the process, waiting for the process to complete, checking the exit status of the process, and destroying (killing) the process.

  • ProcessBuilder − The ProcessBuilder class is used to create operating system processes.This class is not synchronized.

  • Runtime − The Runtime class allows the application to interface with the environment in which the application is running.

  • RuntimePermission − The RuntimePermission class is for runtime permissions. A RuntimePermission contains a name (also referred to as a "target name") but no actions list; you either have the named permission or you don't.

  • SecurityManager − The SecurityManager class allows applications to implement a security policy. It allows an application to determine, before performing a possibly unsafe or sensitive operation, what the operation is and whether it is being attempted in a security context that allows the operation to be performed. The application can allow or disallow the operation.

  • Short − The Short class wraps a value of primitive type short in an object. An object of type Short contains a single field whose type is short.

  • StackTraceElement − The StackTraceElement class element represents a single stack frame. All stack frames except for the one at the top of the stack represent a method invocation.

  • StrictMath − The StrictStrictMath class contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions.

  • String − Strings, which are widely used in Java programming, are a sequence of characters. In Java programming language, strings are treated as objects. The Java platform provides the String class to create and manipulate strings.

  • StringBuffer − The StringBuffer class is mutable sequence of characters. StringBuffer can be used to modify the content of a String with ease. It provides many utility functions to manipulate a string.

  • StringBuilder − The StringBuilder class is mutable sequence of characters. This provides an API compatible with StringBuffer, but with no guarantee of synchronization.

  • System − The System class contains several useful class fields and methods. It cannot be instantiated.

  • Thread − The Thread class is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.

  • ThreadGroup − The ThreadGroup class represents a set of threads. It can also include other thread groups. The thread groups form a tree in which every thread group except the initial thread group has a parent.

  • ThreadLocal − The ThreadLocal class provides thread-local variables.

  • Throwable − The Throwable class is the superclass of all errors and exceptions in the Java language. Only objects that are instances of this class (or one of its subclasses) are thrown by the Java Virtual Machine or can be thrown by the Java throw statement.

  • Void − The Void class is an uninstantiable placeholder class to hold a reference to the Class object representing the Java keyword void.

Examples of java.lang Package

Practice the following examples to learn the concept and usage of java.lang package clasess.

Example of java.lang.Integer

The following program illustrates use of valueOf() method supported by Integer −

package com.tutorialspoint;

public class IntegerDemo {

   public static void main(String[] args) {

      // create a String s and assign value to it
      String s = "+120";

      // create a Integer object i
      Integer i;

      // get the value of int from string
      i = Integer.valueOf(s);

      // print the value
      System.out.println( "Integer value of string " + s + " is " + i );
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Integer value of string +120 is 120

Example of java.lang.Thread

The following program illustrates use of methods supported by Thread −

package com.tutorialspoint;

public class ThreadDemo implements Runnable {

   ThreadDemo() {
      // main thread
      Thread currThread = Thread.currentThread();
      
      // thread created
      Thread t = new Thread(this, "Admin Thread");
   
      System.out.println("current thread = " + currThread);
      System.out.println("thread created = " + t);
      
      // this will call run() function
      t.start();
   }

   public void run() {
      System.out.println("This is run() method");
   }

   public static void main(String args[]) {
      new ThreadDemo();
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

current thread = Thread[main,5,main]
thread created = Thread[Admin Thread,5,main]
This is run() method

Example of java.lang.Math

The following program illustrates use of methods supported by Math −

package com.tutorialspoint;

public class MathDemo {

   public static void main(String[] args) {

      // get two double numbers
      double x = 60984.1;
      double y = -497.99;

      // get the natural logarithm for x
      System.out.println("Math.log(" + x + ")=" + Math.log(x));

      // get the natural logarithm for y
      System.out.println("Math.log(" + y + ")=" + Math.log(y));

      // get the max value
      System.out.println("Math.max(" + x + ", y" + ")=" + Math.max(x,y));

      // get the min value
      System.out.println("Math.min(" + x + ", y" + ")=" + Math.min(x,y));

   }
}	

Output

Let us compile and run the above program, this will produce the following result −

Math.log(60984.1)=11.018368453441132
Math.log(-497.99)=NaN
Math.max(60984.1, y)=60984.1
Math.min(60984.1, y)=-497.99

When java.lang Package is Used?

Whenever we need to prepare UI, threading, use wrapper classess etc, we can rely on classes present in java.lang package. This reference has been prepared for the beginners to help them understand the basic functionality related to all the methods available in Java.lang package.

Prerequisites

Before you start doing practice with various types of examples given in this reference, I'm making an assumption that you are already aware of basic Java Programming.

Advertisements