Data in a Java application is either an object or a primitive data type. There are situations
in Java where only an object can be used and primitive types do not work. For example, the
classes in the Collections API can only hold Object references. The wrapper classes provide
the ability to treat primitive types as objects by wrapping the primitive in an Object . This
section discusses how to wrap a primitive type into its corresponding wrapper class.The wrapper classes are defined in the java.lang package and are used in
situations where an object is required but the data is a primitive type. The primitive type is
“ wrapped ” into an object and can be “ unwrapped ” whenever the primitive value is needed.
There is a wrapper class in the java.lang package for each of the eight primitive types:
Byte This type wraps a byte .
Short This type wraps a short .
Integer This type wraps an int .
Long This type wraps a long .
Float This type wraps a float .
Double This type wraps a double .
Character This type wraps a char .
Boolean This type wraps a boolean .
Wrapper classes have the following properties:
#Each of the wrapper classes contains a single field that holds the value it is wrapping.
#The value of the wrapped primitive type cannot be changed.
#Each class has a constructor that takes in the data type it wraps.
#Except for Character , each class has a constructor that takes in a String that is
#automatically parsed into the corresponding primitive type.
#Each wrapper class has a “ value ” method that unwraps the primitive type. For
example, the Float class has a floatValue method that returns the float .
Let ’ s look at an example. The following code wraps an int into an Integer object:
int x = 357;
Integer w = new Integer(x);
The Integer class has a method named intValue that unwraps the int :
int y = w.intValue();
in Java where only an object can be used and primitive types do not work. For example, the
classes in the Collections API can only hold Object references. The wrapper classes provide
the ability to treat primitive types as objects by wrapping the primitive in an Object . This
section discusses how to wrap a primitive type into its corresponding wrapper class.The wrapper classes are defined in the java.lang package and are used in
situations where an object is required but the data is a primitive type. The primitive type is
“ wrapped ” into an object and can be “ unwrapped ” whenever the primitive value is needed.
There is a wrapper class in the java.lang package for each of the eight primitive types:
Byte This type wraps a byte .
Short This type wraps a short .
Integer This type wraps an int .
Long This type wraps a long .
Float This type wraps a float .
Double This type wraps a double .
Character This type wraps a char .
Boolean This type wraps a boolean .
Wrapper classes have the following properties:
#Each of the wrapper classes contains a single field that holds the value it is wrapping.
#The value of the wrapped primitive type cannot be changed.
#Each class has a constructor that takes in the data type it wraps.
#Except for Character , each class has a constructor that takes in a String that is
#automatically parsed into the corresponding primitive type.
#Each wrapper class has a “ value ” method that unwraps the primitive type. For
example, the Float class has a floatValue method that returns the float .
Let ’ s look at an example. The following code wraps an int into an Integer object:
int x = 357;
Integer w = new Integer(x);
The Integer class has a method named intValue that unwraps the int :
int y = w.intValue();
No comments:
Post a Comment