Wrapper Class In Java

Wrapper Class In Java :

  • Wrapper classes can be used to achieve polymorphism.
  • It is allows primitive data types to be accessed as an objects.
  • All Wrapper class contained in the java.lang package.
  • Wrapper classes are immutable. Once value is assign to it we can not be change value.
  • Vector can not be use primitive data types like int, float, char long so that primitives data type may be converted into object data types by using wrapper classes.
  • Primitive type can not be null but wrapper class can be null.
  • Wrapper class object can store value of NULL.
  • Wrapper class that encapsulates a single, immutable value.
  • All the wrapper classes can be constructed by passing the value to be wrapped into the constructor, most of these constructors throw NumberFormat Exception.
  • In every wrapper class toString() is overriden to return it’s content and equal() is overriden for content composition.
  • Wrapper class support primite data type first character capital. For example: Integer.
  • Class not convert to valueOf(). For example: Void
  • Al Wrapper classes method static.
  • Wrapper class does not contain constructors.
  • autoboxing and unboxing feature convert primitives into objects and objects into primitives automatically. The automatic conversion of primitive into an object is known as autoboxing and vice-versa unboxing.
Wrapper Class In Java

Wrapper class

Boolean, Byte, Character, Double, Float, Integer, Long, Short, Void

Boolean

  • Boolean class define 2 constructors. 1) boolean primitive 2) String argument.
  • boolean primitive only allowed values true or flase where case should be lower case.
  • static boolean getBoolean (String str): Return true if the property by str is true. If the property does not exit or it is flase, it return false.

Boolean boolAllow = new Boolean(true);
Boolean boolAllow = new Boolean(false);
Boolean boolAllow = new Boolean(“True”);
Boolean boolAllow = new Boolean(“True1”);

Short :

Short class wraps a value of primitive data type ‘short’ in its Object. Short class object contains a short type field that stores a short number.

Constuctor
1) Short(short numb)
2) Short(String str)

method
1) int compareTo(Short obj)
2) boolean equals(Object obj)
3) static short parseBoolean(String str)
4) String toString()
5) static short valueOf(String str)

short s = 10;
Short s1 = new Short(s);

String str = “Flutter”;
Short s2 = new Short(str);

Long :

  • The Long class contains a primitive ‘long’ type data. The object of Long class contains a field where we can store a long value.

constructor
1) Long(long l): It takes long number as its parameter and converts it into Long class object.
2) Long(String s): This takes String as its parameter and converts it into its corresponding Long class object.

method
1) int compareTo(long obj)
2) boolean euqals(Object obj)
3) static Long parseInt(String s)
4) String toString()
5) static Long valueOf(String s)
6) long longValue()

Character

Character class define only one constructor whiahc can tacke char primitive as argument there is no String argument constructor.

Constructor
1) Character(char c)

Character ch = new Character(‘a’) //Valid
Character ch = new Character(“A”) //Invalid

Byte

Byte class wraps a primitive byte type value in an object. Its object contains only a single field whose type is byte.

Integer :

Integer class is a wrapper class of int data type.

Method
1) ValueOf(String) : To convert string value into integer object.
2) Int value(): To covert integer object to int variable.
3) Float value(): To convert float object to float variable.
4) Double value(): To convert double object to double variable.
5) parseInt(String): To convert String to int variable.
6) toBinaryString(): To covert String to binary.
7) toHexString(): To convert string to hexa number.
8) toOctString(): To convert to octal number.

For example
String s = “120”;
Integer obj = Integer.valueOf(s);
//int a = Integer.parseInt(s);
int a = obj.intValue();

Double :

  • The double class wraps a value of primitive type double into an Double class Object type. Double class contains a double type field which stores primitive double number.

constructor
1) Double(double d)
2) Double(String s)

Float :

Float class wraps a primitive float type value in an object. Its object contains only a single field whose type is float.

Void :

  • void type does not have any value, it does not contain any methods. It has a constant.
  • void is a keyword which is used to return type of a mehtod. When a method return type is void, it means the method is not return anything.
  • Void is a class with private constructor, Hence we can not create the object of such class.

parsexxx()

  • we can use parsexxx() to convert String to corresponding primitive.

Syntax
public static primitive parsexxx(String s)

Example
Booloean boolVoid = Boolean.parseBoolean(“kamal”);
int i = Integer.parseInt(“10”)

valueOf() method

  • We can use valueOf() to create wrapper object for the given primitive or String is alternative to constructor.
  • Every wrapper class except Characater class contians a static valueOf() to create wrapper object for the given String.

Syntax:
public static wrapperclass valueOf(String s)

method
1) static String valueOf(int i)
2) static String valueOf(float f)
3) static String valueOf(long l)
4) static String valueOf(double d)
5) static String valueOf(char c)
6) static String valueOf(boolean b)
7) static String valueOf(Object o)

For example:
Integer.valueOf(“10”);
Double.valueOf(“10.5”);
Boolean.valueOf(“flutter”);