100% found this document useful (1 vote)
588 views

Wrapper Classes

Here is a program to convert an array of Double objects to an array of Integer objects using a user defined method: ```java import java.util.Arrays; public class WrapperClassDemo { public static void main(String[] args) { Double[] doubleArray = {12.34, 45.67, 78.9}; Integer[] integerArray = doubleToInteger(doubleArray); System.out.println(Arrays.toString(integerArray)); } public static Integer[] doubleToInteger(Double[] doubleArray) { Integer[] integerArray = new Integer[doubleArray.length]; for(int i=0; i<doubleArray.length; i++) {

Uploaded by

adarsh raj
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
588 views

Wrapper Classes

Here is a program to convert an array of Double objects to an array of Integer objects using a user defined method: ```java import java.util.Arrays; public class WrapperClassDemo { public static void main(String[] args) { Double[] doubleArray = {12.34, 45.67, 78.9}; Integer[] integerArray = doubleToInteger(doubleArray); System.out.println(Arrays.toString(integerArray)); } public static Integer[] doubleToInteger(Double[] doubleArray) { Integer[] integerArray = new Integer[doubleArray.length]; for(int i=0; i<doubleArray.length; i++) {

Uploaded by

adarsh raj
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Programming in Java

Wrapper Classes

By
Arvind Kumar
Asst. Professor, LPU
Introduction

• Most of the objects collection store objects and not primitive


types.

• Primitive types can be used as object when required.

• As they are objects, they can be stored in any of the collection


and pass this collection as parameters to the methods.
What is Wrapper Class?
• Each of Java's eight primitive data types has a class dedicated to it.

• These are known as wrapper classes, because they "wrap" the primitive
data type into an object of that class.

• They are one per primitive type: Boolean, Byte, Character, Double,
Float, Integer, Long and Short.

• Wrapper classes make the primitive type data to act as objects.

• All of the primitive wrapper classes in Java are immutable i.e. once
assigned a value to a wrapper class instance cannot be changed further.
What is the need of Wrapper Classes?
• To provide a mechanism to “wrap” primitive values in an
object so that the primitives can be included in activities
reserved for objects, like as being added to Collections, or
returned from a method with an object return value.

• To provide an assortment of utility functions for primitives.


Most of these functions are related to various conversions:
converting primitives to and from String objects, and
converting primitives and String objects to and from different
bases (or radix), such as binary, octal, and hexadecimal.
Primitive Data Types and Wrapper Classes
Data Type Wrapper Class

byte Byte

short Short

int Integer

long Long

char Character

float Float

double Double

boolean Boolean
Creating Wrapper Objects with the new
Operator
• Primitive datatype-->Wrapper Class-->Constructor arguments
• boolean--> Boolean--> boolean or String
• byte--> Byte--> byte or String
• char--> Character--> char
• short--> Short--> short or String
• int-->Integer--> int or String
• long--> Long--> long or String
• float-->Float--> float double or String
• double-->Double--> double or String
Difference b/w Primitive Data Type and
Object of a Wrapper Class

• The following two statements illustrate the difference between


a primitive data type and an object of a wrapper class:

int x = 25;

Integer y = new Integer(33);


The first statement declares an int variable named x
and initializes it with the value 25.
• The second statement instantiates an Integer object. The
object is initialized with the value 33 and a reference to the
object is assigned to the object variable y.
• Clearly x and y differ by more than their values:
x is a variable that holds a value;
y is an object variable that holds a reference to an object.

• So, the following statement using x and y as declared above


is not allowed:

int z = x + y; // wrong!
• The data field in an Integer object is only accessible using the
methods of the Integer class.

• One such method is intValue() method which returns an int


equal to the value of the object, effectively "unwrapping" the
Integer object:

int z = x + y.intValue(); // OK!


Auto Boxing in Wrapper class
• Converting a primitive value (an int, for example) into an
object of the corresponding wrapper class (Integer) is called
autoboxing. The Java compiler applies autoboxing when a
primitive value is:

• Passed as a parameter to a method that expects an object of the


corresponding wrapper class.
• Assigned to a variable of the corresponding wrapper class.
Unboxing in Wrapper class
• Converting an object of a wrapper type (Integer) to its
corresponding primitive (int) value is called unboxing. The
Java compiler applies unboxing when an object of a wrapper
class is:

• Passed as a parameter to a method that expects a value of the


corresponding primitive type.
• Assigned to a variable of the corresponding primitive type
Numeric Type Wrapper Classes
• All of the numeric type wrappers inherit the abstract class Number.

• Number declares methods that return the value of an object in each of the different
number formats. These methods are shown here:

byte byteValue( )
double doubleValue( )
float floatValue( )
int intValue( )
long longValue( )
short shortValue( )

• For example, doubleValue( ) returns the value of an object as a double, floatValue( )


returns the value as a float, and so on.

• These methods are implemented by each of the numeric type wrappers.


Methods to Create Wrapper Objects
• Wrapper class-->method Signature-->method arguments
• Boolean--> static Boolean valueOf(…)-->boolean or String
• Character--> static Character valueOf(…)-->Char
• Byte--> static Byte valueOf(…)-->byte, String, or String and radix
• Short--> static Short valueOf(…)-->short, String, or String and radix
• Integer--> static Integer valueOf(…)-->int, String, or String and radix
• Long--> static Long valueOf(…)-->long, String, or String and radix
• Float--> static Float valueOf(…)-->float or String
• Double--> static Double valueOf(…)-->double or String
Xxx parsexxx(String) method
• Methods to Convert Strings to Primitive Types
• Wrapper Class--> Method Signature--> Method Arguments
• Boolean--> static boolean parseBoolean(…)--> String
• Character--> Not Available
• Byte-->static byte parseByte(…)--> String, or String and radix
• Short--> static short parseShort(…)--> String, or String and radix
• Integer--> static int parseInt(…)--> String, or String and radix
• Long--> static long parseLong(…)--> String, or String and radix
• Float--> static float parseFloat(…)--> String
• Double--> static double parseDouble(…)--> double or String
Let’s Do It
• WAP to convert array of objects of Double type into array of
objects of Integer by using user defined method of following
signature:

public static Integer [] doubleToInteger(Double d[])

You might also like