Data Types and Exceptions in Java
Jim White
Java Developer
primitive | Wrapper Class |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
boolean | Boolean |
char | Character |
Integer
holds a single int
primitive valueimport
(more on imports later)Wrapper-type
variable
= primitive-value
;Integer age = 12;
Double cost = 150250.55;
Float interest = 5.5f;
Character grade = 'A';
Boolean isActive = true;
null
)Integer age = null;
Integer age = 12;
System.out.println(age); // Displays 12
int x = age.intValue(); // x is assigned 12
double z = age.doubleValue(); //z is 12.0
String y = age.toString(); // y is assigned "12"
Integer teenAge = 16;
int smaller = age.compareTo(teenAge); // smaller is -1 since 12 < 16
int x = Integer.sum(8,12); // x is 20
int y = Integer.remainderUnsigned(102, 10); // y is 2
int z = Integer.parseInt("123"); // z is 123;
boolean ans = Boolean.parseBoolean("false"); // ans is false
Example static fields on wrapper classes
System.out.println(Integer.MAX_VALUE); // Maximum value an int can have
System.out.println(Integer.MIN_VALUE); // Minimum value an int can have
2147483647
-2147483648
System.out.println(Boolean.TRUE); // Corresponding to the primitive value of true
System.out.println(Boolean.FALSE); // Corresponding to the primitive value of false
true
false
Example static fields on wrapper classes
System.out.println(Character.SPACE_SEPARATOR); // Unicode for the regular space like ' '
System.out.println(Character.LINE_SEPARATOR); // Unicode for line return like '\n'
12
13
Wrapper Method | Returns |
---|---|
Boolean.logicalAnd(boolean a, boolean b) | boolean |
Boolean.logicalOr(boolean a, boolean b) | boolean |
Boolean.parseBoolean(String s) | boolean |
Character.getNumericValue(char ch) | Unicode int value of the char |
Character.isDigit(char ch) | boolean |
Character.isLowerCase(char ch) | boolean |
Character.isWhitespace(char ch) | boolean |
Double.parseDouble(String s) | double |
Double.longValue() | the Double value, rounded down, as a Long |
int score = Integer.parseInt("8");
null
int herAge; // age is 0 by default
Integer hisAge = null;
if (hisAge != null) {
// do something when hisAge is not set
}
Data Types and Exceptions in Java