Tipuri de date și excepții în Java
Jim White
Java Developer

| Primitiv | Clasă wrapper |
|---|---|
| byte | Byte |
| short | Short |
| int | Integer |
| long | Long |
| float | Float |
| double | Double |
| boolean | Boolean |
| char | Character |

Integer conține un singur primitiv de tip int
import (mai multe despre importuri ulterior)Wrapper-type variable = primitive-value;Integer age = 12;
Double cost = 150250.55;
Float interest = 5.5f;
Character grade = 'A';
Boolean isActive = true;
nullInteger 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
Exemple de câmpuri statice ale claselor wrapper
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
Exemple de câmpuri statice ale claselor wrapper
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
| Metodă wrapper | Returnează |
|---|---|
| Boolean.logicalAnd(boolean a, boolean b) | boolean |
| Boolean.logicalOr(boolean a, boolean b) | boolean |
| Boolean.parseBoolean(String s) | boolean |
| Character.getNumericValue(char ch) | Valoarea Unicode int a caracterului |
| Character.isDigit(char ch) | boolean |
| Character.isLowerCase(char ch) | boolean |
| Character.isWhitespace(char ch) | boolean |
| Double.parseDouble(String s) | double |
| Double.longValue() | Valoarea Double, rotunjită în jos, ca Long |
int score = Integer.parseInt("8");
nullint herAge; // age is 0 by default
Integer hisAge = null;
if (hisAge != null) {
// do something when hisAge is not set
}
Tipuri de date și excepții în Java