Unit 2 - Using Objects

2.8 Wrapper Classes: Integer and Double

In Java, wrapper classes provide a way to use primitive data types (int, double, etc.) as objects. This is particularly useful because, in Java, everything revolves around objects. The primitive data types are not part of the object hierarchy, and hence, cannot be used when an object is required, such as in collections like ArrayList. Wrapper classes solve this problem by "wrapping" the primitive data type in an object, allowing primitives to be used in situations where objects are required.
2.8 Wrapper Classes: Integer and Double

In Java, wrapper classes provide a way to use primitive data types (int, double, etc.) as objects. This is particularly useful because, in Java, everything revolves around objects. The primitive data types are not part of the object hierarchy, and hence, cannot be used when an object is required, such as in collections like ArrayList. Wrapper classes solve this problem by “wrapping” the primitive data type in an object, allowing primitives to be used in situations where objects are required.

 

Integer Wrapper Class

The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int.

 

Creating Integer Objects

Before Java 5, explicitly creating Integer objects was necessary:

 
java
 
Integer myInt = new Integer(100);

 

However, starting from Java 5, autoboxing allows automatic conversion between the int primitive type and the Integer object:

 
java
 
Integer myInt = 100; // Autoboxing from int to Integer

 

Commonly Used Integer Methods

parseInt(String s): Converts the string argument to an integer.

 
java
 
int num = Integer.parseInt("123");
 

toString(): Returns a String object representing the value of the Integer.

java
 
String str = myInt.toString(); // Assuming myInt contains the integer 100

 

compareTo(Integer anotherInteger): Compares two Integer objects numerically.

 
java
 
Integer x = 5;
 
Integer y = 10;
 
int comparisonResult = x.compareTo(y); // Returns a negative value since x < y

 

Double Wrapper Class

The Double class wraps a value of the primitive type double in an object. An object of type Double contains a single field whose type is double.

 

Creating Double Objects

Similar to Integer, Double objects could be explicitly created before Java 5:

 
java
 
Double myDouble = new Double(11.5);

With autoboxing, the explicit object creation is not necessary:

 
java
 
Double myDouble = 11.5; // Autoboxing from double to Double

 

Commonly Used Double Methods

 

parseDouble(String s): Converts the string argument to a double.

 
java
 
double num = Double.parseDouble("11.5");

 

toString(): Returns a String object representing the Double’s value.

 
java
 
String str = myDouble.toString(); // Assuming myDouble contains the double 11.5

 

 

compareTo(Double anotherDouble): Compares two Double objects numerically.

java
 
Double x = 5.5;
 
Double y = 10.5;
 
int comparisonResult = x.compareTo(y); // Returns a negative value since x < y

 

Why Use Wrapper Classes?

Use in Java Collections: Java collections such as ArrayList can only store objects, not primitives. Wrapper classes allow primitives to be used as objects.

Utility Methods: Wrapper classes offer a variety of utility methods for converting, comparing, and manipulating numeric values.

Null Values: Wrapper classes allow for null values, providing a way to represent the absence of a value, which is not possible with primitives.

Autoboxing and unboxing seamlessly bridge the gap between primitives and their corresponding wrapper classes, greatly simplifying code and making Java more expressive.

Shares:

Leave a Reply

Your email address will not be published. Required fields are marked *