πŸ“š

Β >Β 

πŸ’»Β 

Β >Β 

πŸ“±

2.8 Wrapper Classes: Integer and Double

5 min readβ€’june 18, 2024

Kashvi Panjolia

Kashvi Panjolia


AP Computer Science AΒ πŸ’»

130Β resources
See Units

Introduction

You already know about primitive data types like int and double. But you probably didn't know that Integer and Double are classes with methods. And yes, the capitalization matters.

The Integer class and the Double class are called wrapper classes. They convert a primitive data type like an int or double to a reference data type, like an object. Why would we need to do this conversion? Sometimes, we want to pass primitive data types to methods that only accept objects as parameters, so we need to wrap our primitive data in an object. This concept will be explored more in Unit 7.

Integer Wrapper Class

The Integer class in Java is a wrapper class for the primitive data type int. It provides several methods that can be used to perform operations on int values. The constructor looks like this:

Integer(int value)

If you wanted to wrap an int in the Integer wrapper class, you would follow the general rules for creating an object:

Integer numBooks = new Integer(10);

Now, numBooks is the name of an object that points to an int of value 10. This way, we can pass a number into a method or class that requires an object. The Integer class also contains two key variables:

  • Integer.MIN_VALUE tells us the minimum number that an int type can store in Java. This value is -2147483648.
  • Integer.MAX_VALUE tells us the maximum number that an int type can store in Java. This value is 2147483647.

One useful method in the Integer class is intValue(). This method returns the primitive int version of the Integer object. It is used to convert an Integer to an int when a method or class requires the primitive data type, not the object Integer.

public int intValue()

Here is how you can use this method:

Integer numPencils = new Integer(32);
int numOfPencils = numPencils.intValue();

Here, we created a new Integer object called numPencils and pointed it to the primitive int value of 32. In the next line, we called the method intValue() on numPencils using dot notation, then stored the result in a new variable called numOfPencils. The value of numOfPencils will be 32, and now the value 32 is stored as a primitive int.




https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-gVQWKOPEiaNk.jpeg?alt=media&token=f828745a-6f11-44f0-964a-067fa30c6302

No, not this kind of wrapper, unfortunately. Image courtesy of Yahoo.



Double Wrapper Classes

The Double class follows much of the same functionality as the Integer class. It is a wrapper class for the primitive data type double. It provides several methods that can be used to perform operations on double values. The constructor looks like this:
Double(double value)
If you wanted to wrap a double in the Double wrapper class, you would follow the general rules for creating an object:
Double height = new Double(6.4);

The numBooks object points to a primitive double value of 6.4 feet. You can use numBooks to pass this primitive double value to any method or class that requires it.

The Double class also has a method for returning the primitive double version of the Double object. It is used to convert a Double to a primitive double when a method or class requires the primitive data type, not the object Double.
public double doubleValue()
Here is how you can use this method:
Double height = new Double(6.4);
double primitiveHeight = height.doubleValue();
Here, we created a new Double object called height and pointed it to the primitive double value of 6.4. In the next line, we called the method doubleValue() on height using dot notation, then stored the result in a new variable called primitiveHeight. The value of primitiveHeight will be 6.4, and now the value 6.4 is stored as a primitive double.

Autoboxing

Autoboxing is the process of automatically converting a primitive data type to its corresponding wrapper class when an object is required. This is done automatically by the Java compiler. The wrapper class is essentially a wrapper, or box, around the primitive data type. Here is an example:

int a = 5;
Integer b = a;

In this example, the int value of a is automatically converted to an Integer object and stored in the y variable. Autoboxing can be useful when you need to pass a primitive data type as an object to a method that expects an object.

Java can also unbox an object, which is the exact opposite of autoboxing. When an Integer object is assigned to a primitive int type, Java will automatically use the primitive int version of the number and assign it to the int variable.

Integer x = new Integer(10);
int y = x;

Here, Java automatically assigns y the value 10 as a primitive int type, even thought 10 was initialized as an Integer object.

Now that you understand wrapper classes and autoboxing, complete these practice problems to test your knowledge. πŸ“

Practice Problems


1. Which of the following is a wrapper class in Java? a. int b. long c. Double d. boolean e. float Answer: c

2. What is the result of the following code?
Integer x = new Integer(10);
int y = x;
System.out.println(y);
a. 10 b. null c. a compilation error d. an exception is thrown at runtime Answer: a

3. What is the result of the following code?
Integer x = new Integer(22);
int y = x.intValue();
System.out.println(y);
a. 22 b. null c. a compilation error d. int Answer: a

4. What is the result of the following code?
Integer x = new Integer(42);
int y = (int) x;
System.out.println(y);
a. 42 b. NullPointerException c. a syntax error d. x Answer: c

5. Which of the following is NOT a correct way to convert a primitive data type to its corresponding wrapper class in Java? a. int x = 10;
Integer y = x; b. int x = 10;
Integer y = new Integer(x); c. int x = 10;
Integer y = Integer.valueOf(x); d. int x = 10;
Integer y = (Integer) x; e. int x = 10;
Integer y = (Integer) x + 1; Answer: d




Browse Study Guides By Unit
βž•Unit 1 – Primitive Types
πŸ“±Unit 2 – Using Objects
πŸ–₯Unit 3 – Boolean Expressions & if Statements
πŸ•ΉUnit 4 – Iteration
βš™οΈUnit 5 – Writing Classes
⌚️Unit 6 – Array
πŸ’ΎUnit 7 – ArrayList
πŸ’»Unit 8 – 2D Array
πŸ–²Unit 9 – Inheritance
πŸ–±Unit 10 – Recursion
🧐Exam Skills

Fiveable
Fiveable
Home
Stay Connected

Β© 2024 Fiveable Inc. All rights reserved.


Β© 2024 Fiveable Inc. All rights reserved.