0% found this document useful (0 votes)
9 views7 pages

Wrapper Class in Java

The document explains the concept of wrapper classes in Java, which convert primitive types into objects and vice versa through autoboxing and unboxing. It also covers the Object class, its methods, and the process of garbage collection in Java, highlighting how unreferenced objects are automatically cleaned up by the garbage collector. Additionally, it provides examples of boxing, unboxing, and the use of the finalize and gc methods.

Uploaded by

jyotikuntal2516
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views7 pages

Wrapper Class in Java

The document explains the concept of wrapper classes in Java, which convert primitive types into objects and vice versa through autoboxing and unboxing. It also covers the Object class, its methods, and the process of garbage collection in Java, highlighting how unreferenced objects are automatically cleaned up by the garbage collector. Additionally, it provides examples of boxing, unboxing, and the use of the finalize and gc methods.

Uploaded by

jyotikuntal2516
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Wrapper class in Java

Wrapper class in java provides the mechanism to convert primitive into object and
object into primitive.

Since J2SE 5.0, autoboxing and unboxing feature converts primitive into object and
object into primitive automatically. The automatic conversion of primitive into object
is known as autoboxing and vice-versa unboxing.

The eight classes of java.lang package are known as wrapper classes in java. The list
of eight wrapper classes are given below:

Primitive Type Wrapper class

boolean Boolean

char Character

byte Byte

short Short

int Integer

long Long

float Float

double Double

class W
{
public static void main(String args[])
{
int a=20;
Integer i=Integer.valueOf(a);
Integer j=a;
System.out.println(a+" "+i+" "+j);
}

class W{
public static void main(String args[]){
//Converting Integer to int
Integer a=new Integer(3);
int i=a.intValue();//converting Integer to int
int j=a;//unboxing, now compiler will write a.intValue() internally

System.out.println(a+" "+i+" "+j);


}}

______________________________________________________________________

Example 1 :-

String a="10";

int p;

p=a; // C.T. Error

p=(int)a; // C.T. Error

p=Ineger.parseInt(a); // correct.

Example 2 :-

String a="ten";

int p;

p=a; // C.T. Error

p=(int)a; // C.T. Error

p=Ineger.parseInt(a); // NumberFormatException is occured.

Example 3 :-

String a="10.5";

float p;

p=a; // C.T. Error

p=(float)a; // C.T. Error

p=Float.parseFloat(a); // correct.
Example 4 :-

double a=10.5;

int p;

p=a; // C.T. Error

p=(int)a; // correct [Example of Typecasting)

Example 5 :-

int a=10;

Integer p=new Integer(10); // correct

======================================================

Boxing:-To convert primitive data type into object called boxing.


=======================================================

UnBoxing:-opposite process of boxing is called unboxing.To


convert object into primitive data type called Unboxing.

Example 1 :-

int a=10;

Integer p=a; // Autoboxing

Integer m=Integer.valueOf(a); // boxing

Example 2 :-

Integer p=new Integer(10)

int a=p; // AutoUnboxing

Integer m=p.intValue(); // Unboxing


Object class in Java
The Object class is the parent class of all the classes in java by default. In other
words, it is the topmost class of java.

The Object class is beneficial if you want to refer any object whose type you don't
know. Notice that parent class reference variable can refer the child class object,
know as upcasting.

Let's take an example, there is getObject() method that returns an object but it can be
of any type like Employee,Student etc, we can use Object class reference to refer that
object.

Methods of Object class


The Object class provides many methods. They are as follows:

Method Description

public final Class getClass() returns the Class class object of this
object. The Class class can further be
used to get the metadata of this class.

public int hashCode() returns the hashcode number for this


object.

public boolean equals(Object obj) compares the given object to this


object.

protected Object clone() throws creates and returns the exact copy
CloneNotSupportedException (clone) of this object.

public String toString() returns the string representation of this


object.

public final void notify() wakes up single thread, waiting on this


object's monitor.
public final void notifyAll() wakes up all the threads, waiting on this
object's monitor.

public final void wait(long causes the current thread to wait for the
timeout)throws InterruptedException specified milliseconds, until another
thread notifies (invokes notify() or
notifyAll() method).

public final void wait(long timeout,int causes the current thread to wait for the
nanos)throws InterruptedException specified milliseconds and nanoseconds,
until another thread notifies (invokes
notify() or notifyAll() method).

public final void wait()throws causes the current thread to wait, until
InterruptedException another thread notifies (invokes notify()
or notifyAll() method).

protected void finalize()throws is invoked by the garbage collector


Throwable before object is being garbage collected.

class A

void disp()

System.out.println("Hello");

class T

public static void main(String k[])

A obj = new A();

A ob2=obj;

obj.disp();
System.out.println(obj.hashCode()); // Address

System.out.println(obj.equals(ob2)); // True

======================================================

Object ob=10; //correct

ob="Ram"; // correct

Java Garbage Collection


In java, garbage means unreferenced objects.

Garbage Collection is process of reclaiming the runtime unused memory


automatically. In other words, it is a way to destroy the unused objects.

To do so, we were using free() function in C language and delete() in C++. But, in
java it is performed automatically. So, java provides better memory management.

Advantage of Garbage Collection


o It makes java memory efficient because garbage collector removes the
unreferenced objects from heap memory.
o It is automatically done by the garbage collector(a part of JVM) so we don't
need to make extra efforts.

How can an object be unreferenced?


There are many ways:

o `By nulling the reference


o By assigning a reference to another
o By annonymous object etc.
1) By nulling a reference:
1. Employee e=new Employee();
2. e=null;

2) By assigning a reference to another:


1. Employee e1=new Employee();
2. Employee e2=new Employee();
3. e1=e2;//now the first object referred by e1 is available for garbage collection

3) By annonymous object:
new Employee();

finalize() method
The finalize() method is invoked each time before the object is garbage collected. This
method can be used to perform cleanup processing. This method is defined in Object
class as:

protected void finalize(){}

gc() method
The gc() method is used to invoke the garbage collector to perform cleanup
processing. The gc() is found in System and Runtime classes.

public static void gc(){}

Note: Garbage collection is performed by a daemon thread called Garbage Collector(GC). This
thread calls the finalize() method before object is garbage collected.

You might also like