📦 Object-Oriented Programming

Library (Wrapper) Classes

⏱ 3 hr4 topicsInteractive
🎯 By the end: You can name the wrapper class for each primitive, explain autoboxing/unboxing, convert Strings to numbers and back, and use Character methods to test characters.

Java provides ready-made library classes for common jobs. Among the most useful are the wrapper classes, which let a primitive value behave like an object and offer handy conversion methods.

1Wrapper classes

Each primitive type has a matching wrapper class — a class that "wraps" the primitive value as an object so it can be used where objects are needed and gain useful methods:

PrimitiveWrapper class
intInteger
doubleDouble
charCharacter
booleanBoolean
long / floatLong / Float
Wrapper class names start with a CAPITAL letter (Integer, Double) and are spelled out fully (Integer, Character), unlike the lowercase primitives (int, char).
Key points
  • A wrapper class turns a primitive value into an object (Integer for int, Double for double, Character for char, Boolean for boolean).
  • Wrapper class names are capitalised and fully spelled (Integer, Character).
  • Wrappers give primitives useful methods and let them be used where objects are required.

2Autoboxing and unboxing

Java converts automatically between a primitive and its wrapper:

  • Autoboxing — primitive → wrapper object, automatically.
  • Unboxing — wrapper object → primitive, automatically.
Integer obj = 25;     // autoboxing: int 25 -> Integer object
int n = obj;          // unboxing: Integer object -> int
Box = put the value into a wrapper object; un-box = take the value back out. "Auto" means Java does it for you.
Key points
  • Autoboxing automatically converts a primitive to its wrapper object.
  • Unboxing automatically converts a wrapper object back to a primitive.
  • Java performs both conversions for you.

3String ↔ number conversion

Input is often read as text, so converting between Strings and numbers is common:

To doUse
String → intInteger.parseInt("42") → 42
String → doubleDouble.parseDouble("3.14") → 3.14
number → StringString.valueOf(42) → "42"
int n = Integer.parseInt("100");     // n = 100
String s = String.valueOf(99);        // s = "99"
Key points
  • Integer.parseInt(str) converts a String to an int; Double.parseDouble for doubles.
  • String.valueOf(number) converts a number to a String.
  • These conversions are essential when input arrives as text.

4Character methods

The Character class has methods to test and convert a single character:

MethodReturns true if / does
Character.isDigit(c)c is a digit 0–9
Character.isLetter(c)c is a letter
Character.isUpperCase(c)c is an uppercase letter
Character.isLowerCase(c)c is a lowercase letter
Character.isWhitespace(c)c is a space/tab/newline
Character.toUpperCase(c)Returns the uppercase form
char c = '7';
boolean d = Character.isDigit(c);     // true
char u = Character.toUpperCase('a');  // 'A'
Key points
  • Character.isDigit/isLetter/isUpperCase/isLowerCase/isWhitespace test what a character is.
  • Character.toUpperCase/toLowerCase convert a character's case.
  • These help when scanning a string character by character.

★ Practical: use the library classes

In BlueJ, write code that:

  1. Reads a number typed as text and converts it with Integer.parseInt, then doubles it.
  2. Autoboxes an int into an Integer and unboxes it back.
  3. Counts how many characters in a word are digits using Character.isDigit.
  4. Converts a lowercase letter to uppercase with Character.toUpperCase.

Ready for the chapter test?

Answer 5 questions. Score 60% or more to mark this chapter complete.

Start the test →

💡 Log in to save your progress and earn the certificate.