Wrapper class - Smart Tech Guides

Latest

Hi this is Sravan Kumar from India. SMART TECH GUIDES is a technical blog. it helps you to learn about Java topics, frameworks and tools like Jenkins, GitHub & related explanations, data base related topics. All these Explanations are explained in simple and understandable manner.

Subscribe Us

SMART TECH GUIDES

Wrapper class

Wrapper class

The classes which are used to represent primitive values as object are called wrapper classes.

Wrapper classes are used in project especially in collections, serialization, synchronization etc.!

under Java.lang package we have 8 wrapper classes one per each primitive type to represent them as object.


Data type's

Wrapper object

byte

Byte

short

Short

int

Integer

long

Long

float

Float

double

Double

char

Character

boolean

Boolean

 

1) Auto Boxing

Converting primitive datatypes into wrapper class object is called auto boxing

 

2) Auto un-boxing

Converting wrapper class object into primitive datatypes is called auto un-boxing

 

Note: auto boxing, unboxing new feature of Java5 program doesn’t  need to write conversion code.

to perform conversion operations, we have different type conversion operations in wrapper class objects.

  

1) primitive data type to wrapper object conversion

    int i = 10;

    Integer j = Integer.valueOf(i);

    //explicitly converting int to Integer.

System.out.println(j);

 

2) wrapper class object into primitive data type conversion

Integer a = Integer.valueOf(10);

      int b = a.intValue();

      //converting Integer to int explicitly

      System.out.println(b);

 

3) string object to wrapper class object conversion

String a = "123";

   System.out.println(“string to integer conversion”+ Integer.valueOf(a));

 
4) String object to primitive datatype conversion

String strObject = "124";

     int i = Integer.parseInt(strObject);

     System.out.println("String to int: " + i); 


5) wrapper class object to string object conversion

     Integer i = 100;

     System.out.println(i);

     // String name = i; //error still it is in int format

     String stringFormat = i.toString();

     String name = stringFormat;

     System.out.println(name);

     // now it is coverted to string format

No comments:

Post a Comment