Wrapper Classes
Wrapper Classes
However, there are situations where numeric values are needed as objects.
Java solves this problem by providing Wrapper Classes, which are a part of the
java.lang package.
The full name of the Integer class is java.lang.Integer, and similarly for other wrapper
classes.
Wrapper Classes wrap a value of the primitive type in an object.
The class provides several methods for converting between the primitive data type and
a String, as well as other constants and methods useful when dealing with a byte.
Wrapper Classes provide tools such as constants (e.g., smallest and largest int values)
and various utility methods.
The Wrapper Constructors create class objects from the primitive types.
For example:
Double d = 5.0;
Double obj = new Double(d);
A Double wrapper object is created by passing the double value in the Double
constructor.
You can convert strings into primitive types using methods of Wrapper Classes.
For example, Integer.parseInt converts a string to an integer, Double.parseDouble
converts a string to a double, Byte.parseByte converts a string to a byte, and so on.
There is also a Void class, but its objects cannot be created.
The major reasons for having these Wrapper Classes are:
Wrapper Classes enable a primitive value to be used as objects.
As objects, they can be used with all types of classes and their methods.
Wrapper Classes provide many ready-to-use utility methods, such as
converting a string containing a primitive type value to the equivalent primitive
form.
For example, a string "10" can be converted into an integer 10 using a Wrapper
Class method.
Primitive data types are passed by value, and objects are passed by reference.
Wrapper Classes facilitate passing primitives by reference as arguments to a method.
Note: Wrapper Classes are final. Once assigned a value, the value of the Wrapper
Classes cannot be changed.
This ensures uniform capabilities across all instances.
Each Wrapper Class has a constructor method that can be used to create objects of that
class.
If you are passing the initial value to a numeric Wrapper Class constructor, ensure that
the string representation is convertible to the corresponding primitive type.
Otherwise, Java will raise a NumberFormatException.
For example, a string "17.25" is a valid string representation, but "Rs. 17.25" is not
valid for converting into an integer value.
A Character class constructor can take values only in the character form, i.e., a single
character enclosed in single quotes (' ').
A Double class constructor can take initial values in the form of a double, a float, or a
compatible string type.
Now comes the valueOf method: The valueOf method is available in most Wrapper
Classes, and its general function is to receive a string representation of a primitive
type and return the equivalent primitive value object.
For example:
Integer.valueOf("12"); // Returns the value as an Integer object with value 12.
Float.valueOf("3.55"); // Returns 3.55f as a Float type object.
Double.valueOf("3.55"); // Returns 3.55d as a Double type object.
When valueOf is used with the Boolean Wrapper Class, it converts all types of zero
values into false.
For example:
Boolean.valueOf(""); // Converts the empty string into Boolean false.
Boolean.valueOf("false"); // Converts the string "false" into Boolean false.
xxxValue() Methods
These XXX Value Methods are as follows:
Byte value() returns the value of the invoking object as a byte.
Short value() returns the value of the invoking object as a short.
Int value() returns the value of the invoking object as an int.
Long value() returns the value of the invoking object as a long.
Float value() returns the value of the invoking object as a float.
Double value() returns the value of the invoking object as a double.
For example:
Integer obj = new Integer(13);
Long l = obj.longValue();
System.out.println("l = " + l); // It gives 13
Byte Data Type
The byte data type stores values in the range from -128 to 127.
If you try to convert an integer value larger than 127 (or smaller than -128) using a
byte value, the value will be converted into the range of -128 to 127 in a cyclic
fashion.
ParseXXX Methods
These methods convert a valid string representation of a number into the
corresponding numeric types.
For Integer, there is the parseInt method; for Float, there is the parseFloat method, and
so on.
All the parseXXX methods take a string as an argument and convert it into the
corresponding numeric type or throw a NumberFormatException if the string is not
properly formed.
For example:
int num = Integer.parseInt("10"); // Converts the string "10" to an int.
float f = Float.parseFloat("3.14"); // Converts the string "3.14" to a float.
The parse methods of the Wrapper Classes convert the strings to primitive types, while
the valueOf methods return the corresponding Wrapper Class objects from the
primitive types.
toString Methods
The toString methods are a part of all Wrapper Classes and return the string
representation of a value.
For example:
Integer obj = new Integer(13);
String str = obj.toString(); // Converts the Integer object to a string "13"
Character Class
The Character class is a wrapper class for the primitive data type char, and it wraps a
value of the primitive data type char in an object.
The Character class provides a charValue() method that represents the char value
contained in the Character object.
Methods of the Character Class:
static boolean isDigit(char ch) — Returns true if the character is a digit.
static boolean isLetter(char ch) — Returns true if the character is a letter.
static boolean isLetterOrDigit(char ch) — Returns true if the character is
either a letter or a digit.
boolean isLowerCase(char ch) — Returns true if the character is in
lowercase.
boolean isUpperCase(char ch) — Returns true if the character is in
uppercase.
boolean isWhitespace(char ch) — Returns true if the character is a
whitespace.
char toLowerCase(char ch) — Converts the character to lowercase.
char toUpperCase(char ch) — Converts the character to uppercase.
A Character wrapper class is another useful class.
It not only wraps a char type data into the form of an object but also offers many
useful methods.
The individual characters of a string have associated index numbers starting from 0
for the 1st character, 1 for the 2nd character, and n-1 for the nth character.
String.length() represents the number of characters present in a string.