Wrapper Class in Java -> java online - javaengineers

Programming languages or concepts
0
 Introduction to Wrapper Classes in Java

in this wrapper classes in java tutorial, we are learning about  Wrapper Classes in java. We have discussed about the Primitive Data Types in previous tutorial . Since the Primitive Data Types can't be directly used as objects that's why Wrapper classes come into picture.

Generic classes work with objects. but it don't support Primitives. 

Introduction to Wrapper Classes in Java

 Wrapper classes are required as they convert primitive data types into objects and objects are really important if we need to change the arguments passed in a method. 

 let's discuss in detail about the Wrapper Classes in java.

The Java programming language come up with the java.lang package, which has classes that are basic to the design and the most important classes among them are Object and Class.

So, Java wrapper classes wraps the values of primitive data types as an object. When an object is created to a wrapper class, it accommodate a field which can store the primitive data types.

The object of one type contains a field of that specific type only or same type, which means a Double type of object accommodate double type of field only, representing that value so that a reference to it can be stored in a variable of reference type.


Wrapper classes in Java

Wrapper classes provide a way to use primitive data types (int, boolean, char, etc..) as objects.

The wrapper class in Java provides the system to convert primitive into object and object into primitive.

wrapper classes give a way to use primitive data types (int, char, short, byte, etc) as objects. These wrapper classes declare in java.util package.

Since, J2SE 5.0, auto-boxing and unboxing feature convert primitives into objects and objects into primitives automatically. The automatic conversion of primitive into an object is known as auto-boxing and vice-versa unboxing.

Use of Wrapper classes in Java

Java is an object-oriented programming language, so we need to deal with objects many times like in Collections, Serialization, Synchronization, etc. Let us see the different scenarios, where we need to use the wrapper classes.

Change the value in Method: Java supports only call by value. So, if we pass a primitive value, it will not change the original value. But, if we convert the primitive value in an object, it will change the original value.

Serialization: We need to convert the objects into streams to perform the serialization. If we have a primitive value, we can convert it in objects through the wrapper classes.

Synchronization: Java synchronization works with objects in Multithreading.

java.util package: The java.util package provides the utility classes to deal with objects.

Collection Framework: Java collection framework works with objects only. All classes of the collection framework (ArrayList, LinkedList, Vector, HashSet, LinkedHashSet, TreeSet, PriorityQueue, ArrayDeque, etc.) deal with objects only.

Need of Wrapper Classes

1. They convert primitive data types into objects. Objects are needed if we wish to modify the arguments passed into a method (because primitive types are passed by value).

2. The classes in java.util package handles only objects and hence wrapper classes help in this case also.

3. The classes in java.util package handles only objects and hence wrapper classes help in this case also.

4. Data structures in the Collection framework such as ArrayList and Vector store only the objects (reference types) and not the primitive types.

5. The object is needed to support synchronization in multithreading.

6. Data structures in the Collection framework, such as ArrayList and Vector, store only objects (reference types) and not primitive types.

7. An object is needed to support synchronization in multithreading.


there are 8 wrapper classes in java

The eight classes of the 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


Implementation of the wrapper class in Java

Autoboxing in Wrapper Class

Autoboxing is used to convert primitive data types into corresponding objects.

 Automatic conversion of primitive types to the object of their corresponding wrapper classes is known as autoboxing. For example – conversion of int to Integer, long to Long, double to Double etc.

Example


public class AutoBoxingTest {

   public static void main(String args[]) {

      int num = 99; // int primitive

      Integer temp = Integer.valueOf(num); // creating a wrapper class object

      System.out.println(num + " " + temp);

   }

}


OUTPUT:

java -cp /tmp/KiBmZBjRN0 AutoBoxingTest

99 99


Unboxing in Wrapper Class

Unboxing is used to convert the Wrapper class object into corresponding primitive data types.

 It is just the reverse process of autoboxing. Automatically converting an object of a wrapper class to its corresponding primitive type is known as unboxing. For example – conversion of Integer to int, Long to long, Double to double, etc.

Example


public class UnboxingTest {

   public static void main(String args[]) {

      Integer temp = new Integer(99); // Creating Wrapper class object

      int num = temp.intValue(); // Converting the wrapper object to primitive datatype

      System.out.println(num + " " + temp);

   }

}


OUTPUT:

java -cp /tmp/KiBmZBjRN0 UnboxingTest

99 99


Creating Wrapper Objects

Using a Wrapper Class Constructor

We can create a wrapper object using the wrapper class and its constructor by passing the value to it.

Syntax:


ClassName object = new ClassName(argument);



Using Wrapper class only (instead of the primitive type)

We can create without using constructor as well by using the wrapper class instead of the primitive type to create a wrapper object without passing the value to the constructor like we did earlier method. And to get the value, we can print the particular object.
Syntax:

ClassName object = value;                 


Using valueOf Static methods
By using valueOf Static method, a Wrapper object can be created.
Syntax:

ClassName object = ClassName.valueOf(argument);



Features of Java Wrapper Classes

Value modification in function:
We have the 'call by value' function in Java programming, using this we can modify the arguments passed into a method with the help of the objects converted from primitive data types. In many scenarios the argument is not constant it needs to be modified then we can pass the objects and can modify the values accordingly.

Synchronization:
To support Java synchronization an object is needed. It operates into objects in multi-threading. As for the identification of the blocks in multi-threading, objects are required.

Synchronized blocks in Java are marked with the Synchronized keyword. This block in Java is synchronized on some object. All blocks that are synchronized on the same object can only have one thread executing inside them at a time. All other threads attempting to enter the synchronized block are blocked until the thread inside the synchronized block exits the block.

Serialisation:
To implement the serialisation, the object is converted within streams. The object can be regerated using the java wrapper classes. Basically, object is must as the Serializable interface must be implemented by the class whose object needs to be persisted.

java.util package:
The implementaton of the utility classes in the java.util package is to match with the objects and wrapper classes help to achieve the same as well.

Collection framework:
Java collection framework classes such as ArrayList, HashSet, Vector, LinkedList, etc. store only objects i.e. reference types and not primitive types. So objects are instances of wrapper classes and that's why it helps for this.

wrapper class methods in java


Methods Supported by the Wrapper Classes

All of the numeric wrapper classes are subclasses of the abstract class Number such as Byte, Integer, Double, Short, Float, Long.

Some of the frequently used methods that all subclasses of the Number class implements are listed in the following table:

Method

Method Description

typeValue()

Converts the value of this Number object to the specified primitive data type returned

compareTo()

Compares this Number object to the argument

equals()

Determines whether this Number object is equal to the argument

valueOf()

Returns an Integer object holding the value of the specified primitive data type value

toString()

Returns a String object representing the value of specified Integer type argument

parseInt()

Returns an Integer type value of a specified String representation

decode()

Decodes a String into an integer

min()

Returns the smaller value after comparison of the two arguments

max()

Returns the larger value after comparison of the two arguments

round()

Returns the closest round off long or int value as per the method return type

 

wrapper class in java interview questions

What are the Wrapper Classes?
The eight classes of the 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


Why do we need Wrapper classes?
What is boxing?
What is autoboxing?
What is unboxing?
What is auto Unboxing?


Recommanded

Array

Tags:

Post a Comment

0Comments

Post a Comment (0)
close