Wrapper classes

1,478 views 17 slides Jun 16, 2021
Slide 1
Slide 1 of 17
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17

About This Presentation

Java Generics, Collections and Wrapper classes


Slide Content

Java Wrapper Classes Handled By Dr.T.Abirami Associate Professor Department of IT Kongu Engineering College Perundurai

Wrapper classes The wrapper classes in Java are used to convert primitive types ( int , char, float, etc) into corresponding objects.

Need for Wrapper Classes Generic classes only work with objects and don't support primitives . Data   structures  in the Collection framework such as  ArrayList and Vector  store only the objects (reference types) and not the  primitive types. The object is needed to support  synchronization  in  multithreading .

Importance of Wrapper classes There are mainly two uses with wrapper classes. 1)  To convert simple data types into objects, that is, to give object form to a data type; here constructors are used. 2)  To convert strings into data types (known as parsing operations), here methods of type parseYYY () are used. YYY-> datatypes

Features of the Java wrapper Classes. 1)  Wrapper classes convert numeric strings into numeric values. 2)  The way to store primitive data in an object. 3)  The valueOf () method is available in all wrapper classes except Character 4)  All wrapper classes have datatypeValue () method. This method returns the value of the object as its primitive type.

Wrapper Classes To convert primitive data type into object and object into primitive. The automatic conversion of primitive into an object is known as autoboxing objects into primitives automatically is called unboxing

The eight classes of the java.lang package are known as wrapper classes in Java. The list of eight wrapper classes are given below: Wrapper Classes Primitive Type Wrapper class boolean Boolean char Character byte Byte short Short int Integer long Long float Float double Double

Creating Wrapper Objects public class MyClass { public static void main(String[] args ) { Integer myInt = 5; Double myDouble = 5.99; Character myChar = 'A'; System.out.println ( myInt ); System.out.println ( myDouble ); System.out.println ( myChar ); } }

valueOf () method : T o create Wrapper object for given primitive data type or String. parseInt () , parseFloat (), parseDouble () method : to convert String class (Wrapper class) to primitive methods of Wrapper classes

methods of Wrapper classes Method Purpose parseInt(s) returns a signed decimal integer value equivalent to string s toString(i) returns a new String object representing the integer i byteValue() returns the value of this Integer as a byte doubleValue() returns the value of this Integer as a double floatValue() returns the value of this Integer as a float intValue() returns the value of this Integer as an int shortValue() returns the value of this Integer as a short longValue() returns the value of this Integer as a long

Creating Wrapper Objects public class MyClass { public static void main(String[] args ) { Integer myInt = 5; Double myDouble = 5.99; Character myChar = 'A'; System.out.println ( myInt ); System.out.println ( myDouble ); System.out.println ( myChar ); } }

Convert Wrapper class into Primitive data types public class MyClass { public static void main(String[] args ) { Integer myInt = 5; Double myDouble = 5.99; Character myChar = 'A'; System.out.println ( myInt.intValue ()); System.out.println ( myDouble.doubleValue ()); System.out.println ( myChar.charValue ()); } }

autoboxing public class AutoBoxingTest {     public static void main(String args []) {       int num = 10; // int primitive       Integer obj = Integer.valueOf (num); // creating a wrapper class object       System.out.println (num + " " + obj );     } }

Unboxing public class UnboxingTest {     public static void main(String args []) {       Integer obj = new Integer(10); // Creating Wrapper class object       int num = obj.intValue (); // Converting the wrapper object to primitive datatype       System.out.println (num + " " + obj );     } }

Primitive data type to Wrapper class // Autoboxing  example of  int  to Integer   class WrapperExample1{   public static void main(String  args []){   //Converting  int  into Integer   int  a=20;   Integer  i = Integer.valueOf (a); //converting  int  into Integer explicitly   Integer j=a; // autoboxing , now compiler will write  Integer.valueOf (a) internally   System.out.println (a+" "+ i +" "+j);   }}  

Wrapper class to Primitive data type // Unboxing  example of Integer to  int    class WrapperExample2{     public static void main(String  args []){     //Converting Integer to  int      Integer a=new Integer(3);     int   i = a.intValue (); //converting Integer to  int  explicitly   System.out.println (a+" "+ i );     }}    

toString () // Java program to illustrate toString ()    class GFG {     public static void main(String[] args )     {         Integer I = new Integer(10);         String s = I.toString ();          System.out.println (s);     } }
Tags