Careeroza
Wrapper Classes
Objects for Primitives
Each primitive type has a wrapper class in java.lang. Wrappers are needed because collections and generics work only with objects: you cannot write List<int>, only List<Integer>.
| Primitive | Wrapper | Example |
|---|---|---|
byte | Byte | Byte.MAX_VALUE = 127 |
short | Short | |
int | Integer | Integer.parseInt("42") |
long | Long | Long.parseLong("9000000000") |
float | Float | |
double | Double | Double.parseDouble("3.14") |
char | Character | Character.isDigit('7') |
boolean | Boolean | Boolean.parseBoolean("true") |
Useful Constants and Methods
Example
public class Main {
public static void main(String[] args) {
System.out.println("int range: " + Integer.MIN_VALUE + " to " + Integer.MAX_VALUE);
System.out.println("max of 7, 3: " + Integer.max(7, 3));
System.out.println("compare(5, 9): " + Integer.compare(5, 9));
System.out.println("binary of 10: " + Integer.toBinaryString(10));
System.out.println("hex of 255: " + Integer.toHexString(255));
System.out.println("isLetter('a'): " + Character.isLetter('a'));
System.out.println("toUpperCase('q'): " + Character.toUpperCase('q'));
System.out.println("Double.isNaN(0.0/0): " + Double.isNaN(0.0 / 0));
}
}Output
int range: -2147483648 to 2147483647
max of 7, 3: 7
compare(5, 9): -1
binary of 10: 1010
hex of 255: ff
isLetter('a'): true
toUpperCase('q'): Q
Double.isNaN(0.0/0): true