Monday, November 26, 2007

What is in java.lang package?

The lang package

The java.lang package provides classes that are fundamental to the Java programming language.
java.lang is automatically imported in all programs. It is java's most widely used package.

java.lang package provides 29 classes and three interfaces. Due to its importance, the java.lang package is provided with all Java platforms ranging from Embedded Java to the JDK.

Interfaces in java.lang

The java.lang package contains three interfaces namely

  • Cloneable
  • Comparable
  • Runnable

Cloneable : A class implements Cloneable to indicate the Object.clone() method that it is authorized to make a copy of instances of that class.

The exception CloneNotSupportedException will be thrown if the user clones instances, which do not implement the Cloneable interface. The interface Cloneable declares no methods.

Comparable : Java 2 adds a new interface to java.lang called Comparable. Classes that implement Comparable interface contain objects that can be compared with each other.

The comparable method declares only one method. The syntax is:

int compareTo(Object obj)

this method compares the invoking object with 'obj'. It returns 0 if the values are equal.

Runnable : Any class that initiates a separate thread of execution must implement the Runnable interface.

Runnable defines only one abstract method, called run(), which is the entry point to the thread. Threads that are created by the user must implement abstract void run() method.

No comments: