Wrapper class in JavaWrapper class in java are the Object representation of eight primitive types in java. All the wrapper classes in java are immutable and final.Java 5 autoboxing and unboxing allows easy conversion between primitive types and their corresponding wrapper classes in java programs.Below table shows the primitive types and their wrapper class in java.Primitive type     Wrapper class     Constructor Argumentsbyte                         Byte                       byte or Stringshort                       Short                     short or Stringint                             Integer                  int or Stringlong                          Long                      long or Stringfloat                          Float                     float, double or Stringdouble                      Double                 double or Stringchar                          Character           charboolean                   Boolean               boolean or StringWhy do we need wrapper classes?I think it was a smart decision to keep primitive types and Wrapper classes separate to keep things simple. We need wrapper classes when we need a type that will fit in the Object-Oriented Programming like Collection classes. We use primitive types when we want things to be simple. package com.mapack.mk;import java.util.ArrayList;import java.util.List;WrapperClasses.java Examples : public class WrapperClasses {    private static void doSomething(Object obj){            }        public static void main(String args[]){        int i = 10;        char c = 'a';                //primitives are simple to use        int j = i 3;                //polymorphism achieved by Wrapper classes, we can't pass primitive here        doSomething(new Character(c));                List list = new ArrayList();        //wrapper classes can be used in Collections        Integer in = new Integer(i);        list.add(in);                //autoboxing takes care of primitive to wrapper class conversion        list.add(j);                //wrapper classes can be null        in = null;    }} - Study24x7
Social learning Network
study24x7

Default error msg

Login

New to Study24x7 ? Join Now
Already have an account? Login
05 Mar 2019 12:37 PM study24x7 study24x7

Wrapper class in Java
Wrapper class in java are the Object representation of eight primitive types in java. All the wrapper classes in java are immutable and final.

Java 5 autoboxing and unboxing allows easy conversion between primitive types and their corresponding wra...

See more

study24x7
Write a comment
Related Questions
500+   more Questions to answer
Most Related Articles