Tipi di dati ed eccezioni in Java
Jim White
Java Developer

| primitivo | Classe wrapper |
|---|---|
| byte | Byte |
| short | Short |
| int | Integer |
| long | Long |
| float | Float |
| double | Double |
| boolean | Boolean |
| char | Character |

Integer contiene un singolo valore primitivo int
import (più avanti sugli import)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); // Mostra 12
int x = age.intValue(); // x vale 12
double z = age.doubleValue(); // z è 12.0
String y = age.toString(); // y diventa "12"
Integer teenAge = 16;
int smaller = age.compareTo(teenAge); // smaller è -1 poiché 12 < 16
int x = Integer.sum(8,12); // x è 20
int y = Integer.remainderUnsigned(102, 10); // y è 2
int z = Integer.parseInt("123"); // z è 123
boolean ans = Boolean.parseBoolean("false"); // ans è false
Esempi di campi statici nelle classi wrapper
System.out.println(Integer.MAX_VALUE); // Valore massimo di un int
System.out.println(Integer.MIN_VALUE); // Valore minimo di un int
2147483647
-2147483648
System.out.println(Boolean.TRUE); // Corrisponde al valore primitivo true
System.out.println(Boolean.FALSE); // Corrisponde al valore primitivo false
true
false
Esempi di campi statici nelle classi wrapper
System.out.println(Character.SPACE_SEPARATOR); // Unicode per lo spazio normale come ' '
System.out.println(Character.LINE_SEPARATOR); // Unicode per il ritorno a capo come '\n'
12
13
| Metodo wrapper | Restituisce |
|---|---|
| Boolean.logicalAnd(boolean a, boolean b) | boolean |
| Boolean.logicalOr(boolean a, boolean b) | boolean |
| Boolean.parseBoolean(String s) | boolean |
| Character.getNumericValue(char ch) | valore int Unicode del char |
| Character.isDigit(char ch) | boolean |
| Character.isLowerCase(char ch) | boolean |
| Character.isWhitespace(char ch) | boolean |
| Double.parseDouble(String s) | double |
| Double.longValue() | il valore Double, arrotondato per difetto, come Long |
int score = Integer.parseInt("8");
nullint herAge; // age è 0 di default
Integer hisAge = null;
if (hisAge != null) {
// fai qualcosa quando hisAge non è impostata
}
Tipi di dati ed eccezioni in Java