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:
| Primitive | Wrapper class |
|---|---|
| int | Integer |
| double | Double |
| char | Character |
| boolean | Boolean |
| long / float | Long / Float |
- 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
- 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 do | Use |
|---|---|
| String → int | Integer.parseInt("42") → 42 |
| String → double | Double.parseDouble("3.14") → 3.14 |
| number → String | String.valueOf(42) → "42" |
int n = Integer.parseInt("100"); // n = 100
String s = String.valueOf(99); // s = "99"
- 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:
| Method | Returns 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'
- 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:
- Reads a number typed as text and converts it with Integer.parseInt, then doubles it.
- Autoboxes an int into an Integer and unboxes it back.
- Counts how many characters in a word are digits using Character.isDigit.
- 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.