Вы находитесь на странице: 1из 8

Chapter

34

Wrapper Classes
Everything in Java is an object well, almost everything in Java is an object except for the eight primitive data types. Many times, it is important to treat the values in the eight primitive data types as if they are classes (objects). Java provides eight classes for this. These classes are called wrapper classes, since they wrap primitive values and behavior inside the garb of a class. In most cases the name of the wrapper class is the same as the name of the primitive data type, except that the case of the first letter is upper-case. For example, the wrapper class for byte is Byte. The difference in names exists for the int and char primitives, where the wrapper classes are named Integer and Character, respectively. And please dont ask me why!!!

734

Java For Students

Wrapper classes are also known as type-wrapper classes. This is because they are classes for the eight primitive data types only. The eight wrapper classes are: Byte, Short, Integer, Long, Float, Double, Boolean, and Character.

Creating objects of wrapper classes


In order to create an object of a wrapper class, we use the new keyword. Every wrapper class has a constructor that accepts a value of that primitive data type as a parameter. For example, Integer I = new Integer( 100 ) will create an object I of the wrapper class Integer that holds the int value 100.

Deriving the primitive value from a wrapper object


In order to get an int value from an Integer object, we use the intValue() method of the wrapper class. For example, int x = I.intValue() will return the int value stored inside the Integer wrapper object I. Similar to the intValue() method, the other wrapper classes have corresponding methods: byteValue(), shortValue(), longValue(), floatValue(),

doubleValue(), charValue(), and booleanValue(). Create a file Wrapper1.java as shown below:


/*

Wrapper Classes

735

File name: Wrapper1.java Class : Wrapper Purpose : Introduction to wrapper classes */ class Wrapper1 { public static void main( String[] args ) { Integer I = new Integer( 100 ); int x = I.intValue(); System.out.println( "I: " + I ); // both print 100 System.out.println( "x: " + x ); // both print 100 } }

Save the file, compile it, and execute the class file. You will get this:

You will observe above that even printing-wise, when you print the wrapper object the value that gets printed is the primitive value held by that object.

Overloaded constructor of wrapper classes


Wrapper classes have an overloaded constructor: one that accepts a String value as a parameter. Internally, it converts the String to its primitive value. Create a file Wrapper2.java as shown below:
/*

736

Java For Students

File name: Wrapper2.java Class : Wrapper Purpose : Introduction to wrapper classes */ class Wrapper2 { public static void main( String[] args ) { Integer I = new Integer( "100" ); int x = I.intValue(); System.out.println( "I: " + I ); // both print 100 System.out.println( "x: " + x ); // both print 100 Double D = new Double( "123.45" ); double y = D.doubleValue(); System.out.println( "D: " + D ); // both print 123.45 System.out.println( "y: " + y ); // both print 123.45 Boolean B = new Boolean( "true" ); boolean b = B.booleanValue(); System.out.println( "B: " + B ); // both print true System.out.println( "b: " + b ); // both print true } }

Save the file, compile it, and execute the class file. You will get this:

Wrapper Classes

737

Autoboxing and auto-unboxing


When you consider wrapper classes, creating a new object each time seems too much of work and effort. Instead, Java allows us to directly write something like Integer I = 100. Internally, Java converts the literal 100 into the wrapper object. The process of converting a primitive data type value into its equivalent wrapper class is known as autoboxing.

Similarly, when we want to convert a wrapper class into a primitive value, we can do without the use of methods such as I.intValue() by using something like this: int x = I. Java takes care of calling the appropriate method internally and automatically. The process of extracting a primitive data type value from its wrapper class object is known as auti-unboxing. It is the opposite of autoboxing.

Java automatically performs autoboxing and auto-unboxing. Autoboxing is performed whenever a wrapper class is required and a primitive data type value is provided. Auto-unboxing is performed whenever a primitive data type value is required and a wrapper class object is provided. These conversions are automatically done by Java. Create a file Wrapper3.java as shown below:
/* File name: Wrapper3.java Class : Wrapper Purpose : Introduction to wrapper classes */

738

Java For Students

class Wrapper3 { public static void main( String[] args ) { Integer I1 = 1, I2 = 2, I3; I2++; // I2 is now 3 I3 = I1 + I2; // I3 = 1 + 3 = 4 int x = I1 + I2 + I3; // x = 1 + 3 + 4 = 8 System.out.println( "I1: " + I1 ); System.out.println( "I2: " + I2 ); System.out.println( "I3: " + I3 ); System.out.println( "x : " + x ); } }

Save the file, compile it, and execute the class file. You will get this:

Theory questions
Q1. What do you understand by wrapper classes? A1. Everything in Java is an object almost except for the eight primitive data types. Sometimes, it is necessary to treat the primitive data values as objects. For example, when adding them to a collection.

Wrapper Classes

739

Wrapper classes encompass primitive data type values inside an object. There are eight wrapper classes, one for each primitive data type. They are: Byte, Short, Integer, Long, Float, Double, Boolean, and Character.

Q2. Explain what you understand by autoboxing and auto-unboxing. A2. Autoboxing and auto-unboxing provide for automatic conversion between primitive data type values and their wrapper class equivalents. The process of converting a primitive data type value into its equivalent wrapper class is known as autoboxing. The process of extracting a primitive data type value from its wrapper class object is known as auti-unboxing. It is the opposite of autoboxing. An example of autoboxing is: Integer I = 100; // I is now an Integer object with the primitive value 100. An example of auto-unboxing is: int x = I; // the value 100 gets into x

Q3. Which of the following are not wrapper classes? a. Double b. Triple c. Int d. Byte

740

Java For Students

e. Char f. VeryLong g. Boolean A3. Triple, Int, Char, and VeryLong are not wrapper class objects.

Вам также может понравиться