Blog Image

Java.lang Package

 



by


Swaraj Ghadage


Java Developer At Riyality Software Services Sangola……


This is default package of java.if we use some classes from java.lang we need not import because it default package contains Object class,String class,StringBuffer class,StringBuilder class etc.


Object class:


Object class is default parent of every java class.This class contains some methods which are required in every java class.That is why they have declared this class as default parent.Object class has following methods.


Description: IMG_256


1.      toString()


2.      hashcode()


3.      equals()


4.      finalize()


5.      getClass()


6.      clone()


7.      wait()


8.      wait(int milliseconds)


9.      wait(int milliseconds)


10.  notify()


11.  notifyAll()


1.toString():-


This methods returns string representation of object.when we print reference variable ,internally JVM calls toString() method.


eg. public class Demo{


int a =10;


public static void main(String[]args){


Demo d =new Demo();


System.out.println(d);


}


} here when print ‘d’ variable internally it will calls toString method as given below. System.out.println(d.toString());


Whenever we print any references variable,it will print classname@hashcode in hexadecimal format. it will not print data in that object.


2.hashCode()method:


Using this method we can get the hashcode of an object.Hashcode is a unique value which we assign to object.


public class Demo{


public static void main(String[]args){


Demo d =new Demo();


System.out.println(d.hashCode());


} o/p: 1234567 that is some hashcode


3.Equals method:


Equals method is used to compare references of an objects.


public class Demo{


public static void main(String[]args){


Demo d =new Demo();


Demo d1 =new Demo();


System.out.println(d.equals(d1));


}


Here we will get output false.Here ,both the objects are having same data still equals method will return false because equals method compare references not data.


Difference between == and equals method:


1.== :- it is a operator to compare normal variables,it compares data.if we use == operator to compare variables then it compare references.


2.it is a method present in Object class which is meant to compare references.


4.finalize():-


Garbage collector destroys unused object from memory.But before destroying un reference objects it calls the finalize method to make sure any associated connections are closed. so connections closing inside the finalize method by overriding it.


5.getClass() method:-


This method will return Class class object for that given object.Class class object contains all class level information of a class .


e.g public class Demo{


int a=10;


public static void main(String[]args){


Demo d =new Demo();


Class c =d.getClass();


System.out.println(c.getSuperclass());


}
}


//we can not override getClass method bcoz that is final.


6.clone() method:-


This method is used to create clone of an object.it does shallow cloning not deep cloning.


public class Demo implements Cloneable{


int a =10;


public static void main(String[]args)throws CloneNotSupportedException{


Demo d =new Demo();


Demo d1 =(Demo)d.clone();


system.out.println(d.a);


system.out.println(d1.a);


}}


//if we want deep cloning then we have to override clone method.


7.wait() method:-


The wait() methods in the Object class of the java.Lang package are used in java’s thread synchronization mechanism.these method allow a thread to wait until another thread notifies it that it can proceed.three version of the wait() method.


1.      void wait()


2.      void wait(int milliseconds)


3.      void wait(int timeout ,int nanos)


1.void wait():-This method causes the current thread to wait until indefinitley another thread invokes the notify() or notifyAll() method on the same object.


2.void wait(int timeout):-


this method causes the current thread to wait until another thread invokes notify() or notifyAll() or until the specified timeout(int milliseconds)has elapsed.


3.void wait(int timeout ,int nanos):-


this method is similar to wait(int timeout) but it allows for greater precisions by specifying a timeout period in milliseconds plus nanoseconds .the nanos parameter represents additional time in nanoseconds(0-9999999) the thread wait until it is notified or the timeout period has elapsed.


8.notify() method:-


The notify() are a part of Object class are used in java’s thread synchronization mechanism.


wakes up a single thread that is waiting on the objects monitor,if multiple threads are waiting one of them will be awakened.


typically used when a single thread can proceed with the work and others should remain waiting.


9.notify-all() method:-


wakes up the all threads that are waiting on the object’s monitor.All waiting threads are moved to the ready state and will complete for the objects monitor .typically used when all waiting thread need to proceed.such as in a broadcast scenario where all waiting threads need to respond to a state change.


 

Author Photo

Swaraj Ghadage

Batch Number: Java Batch - 3