Explanation of Wrapper classes in Java

Explanation of Wrapper classes in Java

Hello everyone, in this article we are going to talk about the wrapper classes which are wrapping the primitive types that we discussed before in Java programming language.

Let's begin.

In Java, data types are very important to keep and process the data. In Java we have two data types: primitives and reference types. Primitives are char, double int and etc..., and reference data types are object classes interfaces etc....

Wrapper classes are the classes to wrap the primitive types and offers useful methods to work with these primitive data types in object oriented programming model. We can think wraper classes as a bridge between primitive types and object oriented world.

In Java below list you can see the wrapper classes list.

  • Integer int
  • Long long
  • Double double
  • Float float
  • Character char
  • Boolean boolean
  • Byte byte
  • Short short

Benefits of Wrapper Classes

Below you can see some benefits of wrapper classes:
  • Object-Oriented Compatibility: Wrapper classes integrate the primitive types within the object oriented world and make easier to other Java API's to work together.
  • Null Values: Primitives can not be null as they have their default values if not assigned. But the objects can be assigned to null. Wrapper classes are accepting null values to handle the missing data.
  • Working with Collections: Java collection classes are designed to work with object not with primitive types. So with wrapper classes we are enabled to work with primitive types in collections.
  • Generics: When working with generics it is required to work with reference types. So we can use wrapper classes to handle the primitive data types.
  • Utility Methods: Wrapper classes provide methods for common operations for their primitive data types. These methods helps to operations like type conversion, parsing, and comparison.
  • Methods with Objects: Some methods require objects as arguments. Using wrapper classes allows you to pass primitive values as objects.

Below you can see how to initialise wrapper classes:

We can directly initialise with constructors of wrapper classes


Integer myInteger = new Integer(198);
Double myDouble = new Double(3.14);
Boolean myBoolean = new Boolean(false);

Static Factory Methods


Integer myInteger = Integer.valueOf(198);
Double myDouble = Double.valueOf(3.14);
Boolean myBoolean = Boolean.valueOf(false);

Autoboxing and Unboxing

Java provides the direct conversation between primitive types and their wrapper classes.

  • Autoboxing is initialising the class with directly = operator with respective data.
  • Unboxing is obtaining the primitive data from wrapper class.
Below you can see the example of Autoboxing and Unboxing

// Autoboxing
Integer myInteger = 198;  
Double myDouble = 3.14;  
Boolean myBoolean = true; 

 // Unboxing 
int primInt = myInteger.intValue();
double primDouble = myDouble.doubleValue();
boolean primBool = myBoolean.booleanValue();

That is all for wrapper classes.

Burak Hamdi TUFAN


Tags


Share this Post

Send with Whatsapp

Post a Comment

Success! Your comment sent to post. It will be showed after confirmation.
Error! There was an error sending your comment. Check your inputs!

Comments

  • There is no comment. Be the owner of first comment...