Академический Документы
Профессиональный Документы
Культура Документы
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
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.
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.
736
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
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
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
e. Char f. VeryLong g. Boolean A3. Triple, Int, Char, and VeryLong are not wrapper class objects.