Unit 2: Using Objects

Delving into Java's Wrapper Classes for AP CSA


Introduction to Wrapper Classes:

Definition and Purpose

  • Definition: Java's mechanism to encapsulate primitive data types as objects.
  • Purpose: Indispensable when the situation demands objects, such as in the context of collections, generics, or specific APIs.

Core Wrapper Classes

For every primitive data type in Java, there's a corresponding wrapper class, serving as an object envelope:

Primitive Data TypeWrapper Class
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

Crafting Wrapper Objects

The instantiation of a wrapper object is straightforward. Below is a demonstration with the primitive data type int:

Integer myInt = 5;

Constructors vs. valueOf()

There are primarily two approaches to create a wrapper object:

  • Constructors: Less favored and deprecated for several wrappers, for instance: Integer intObj = new Integer(5);
  • **Using valueOf(): The preferred method: **Integer intObj = Integer.valueOf(5);**

Extracting Primitive Values

Each wrapper class is equipped with methods to retrieve the encapsulated primitive value:

Wrapper ClassPrimitive Extraction Method
BytebyteValue()
ShortshortValue()
IntegerintValue()
LonglongValue()
FloatfloatValue()
DoubledoubleValue()
CharactercharValue()
BooleanbooleanValue()

Autoboxing and Unboxing:

Java's compiler adeptly manages conversions between primitive types and their wrapper class counterparts.

Autoboxing

The automatic transition facilitated by the compiler, where a primitive is converted to its associated wrapper object:

// Autoboxing example
Character ch = 'a';

Unboxing

The inverse of autoboxing, where a wrapper object is reverted to its primitive form:

// Unboxing example
char a = ch;

Interplay of Wrapper Classes and Strings

Transition from String to Wrapper

Here's how a String morphs into an Integer wrapper object:

String myString = "123";
Integer myInt = Integer.valueOf(myString);

Transition from Wrapper to String

And here's the transformation of an Integer wrapper object back into a String:

Integer myInt = 123;
String myString = myInt.toString();

AP CSA Homework Assignment

Assignment

Instructions

  • Create a new Java class called WrapperClasses.
  • In the main() method, create a String variable called myString and assign it the value "123".
  • Create an Integer wrapper object called myInt and assign it the value of myString.
  • Print the value of myInt.
  • Create a String variable called myString2 and assign it the value of myInt.
  • Print the value of myString2.

By using the wrapper classes for Integer and String, you should be able to convert between the two data types. It would benefit you to practice with the other wrapper classes as well:

  • Use the Byte wrapper class to convert between byte and String.
  • Use the Short wrapper class to convert between short and String.
  • Use the Long wrapper class to convert between long and String.
  • Use the Float wrapper class to convert between float and String.
  • Use the Double wrapper class to convert between double and String.
  • Use the Character wrapper class to convert between char and String.
  • Use the Boolean wrapper class to convert between boolean and String.

References

Previous
String Classes