[go: up one dir, main page]

0% found this document useful (0 votes)
15 views5 pages

Wrapper Classes

Java provides Wrapper Classes to allow primitive data types to be treated as objects, enabling their use with classes and methods that require objects. These classes offer utility methods for converting between primitive types and strings, as well as methods for obtaining primitive values from Wrapper objects. Autoboxing and unboxing facilitate the automatic conversion between primitive types and their corresponding Wrapper Class objects.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views5 pages

Wrapper Classes

Java provides Wrapper Classes to allow primitive data types to be treated as objects, enabling their use with classes and methods that require objects. These classes offer utility methods for converting between primitive types and strings, as well as methods for obtaining primitive values from Wrapper objects. Autoboxing and unboxing facilitate the automatic conversion between primitive types and their corresponding Wrapper Class objects.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

In Java, primitive data types are the data values and not the class objects.

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.

Autoboxing and Unboxing


Wrapper classes are the object-oriented form of the primitive type values.
You can assign a primitive value to its Wrapper Class object, and Java will
automatically do it for you (this is known as autoboxing).
Similarly, you can assign a Wrapper Class object's value directly to its corresponding
primitive data type, and Java will automatically do it for you (this is known as
unboxing).
Autoboxing in Java is used to convert primitive data types into their corresponding
objects.
Unboxing in Java is the reverse of autoboxing. It converts a Wrapper Class object into
its corresponding primitive data type.
Example of Autoboxing:
char ch = 'a';
Character characterObj = ch; // Autoboxing: primitive char to Character object
Example of Unboxing:
int val = 10;
Integer obj = val; // Autoboxing: primitive int to Integer object
int newVal = obj; // Unboxing: Integer object to primitive int
With autoboxing and unboxing, you can use Wrapper Class objects in the same way as
primitive data types.
You don't need to do anything specific to convert a primitive data type to its Wrapper
Class object or vice versa.

Comparison of Wrapper Objects


One important thing is that if two objects of the same Wrapper Class have the same
value and you use the comparison operator (such as ==) to compare them, Java will
return false.
This is because Java compares their references (i.e., their memory addresses), not their
values.
Since these two different objects are stored in different locations in memory, Java
produces the above result.
If you use the intValue() method to compare the values, like this:
if (obj1.intValue() == obj2.intValue()) {
// It will return true because the values are being compared
}
This will return true because now the values are being compared, not their references.
It is sometimes easier to deal with primitive types as objects, especially when working
with methods.
Primitive types are passed by value, and if we want changes to be reflected, we need
to pass them as objects.
Objects are necessary if we wish to modify the arguments and pass them into methods.
Additionally, classes defined in the java.util package work with objects only. If we
want to use them with primitive values, we need to convert the primitive values using
the Wrapper Classes.

You might also like