byte
signed 8-bit integer value.short
signed 16-bit integer value.int
signed 32-bit integer value.long
signed 64-bit integer value.float
single-precision 32-bit floating-point value.double
double-precision 64-bit floating-point value.char
single 16-bit Unicode character.boolean
boolean value, which can be either true or false.// Local variable
int bufferSize = 74;
// Local constant
final String APP_NAME = "Engineering Cheat Sheets";
// Instance constant, variable.
public class User {
final String[] GENDER_OPTIONS = {"Male", "Female"};
String name;
int age;
}
// Class constant and variable.
public class Scheduler {
private static final EXECUTION_INTERVAL = 500;
static boolean isRunning;
}
// if-then
if (isLoaded || length > 0) {
...
}
// if-then-else
if (score < 40) {
...
} else if (65 < score && score < 70) {
...
} else {
...
}
// Switch
switch (grade) {
case 'A':
...
break;
case 'B', 'C':
...
break;
...
default:
...
break;
}
// Switch expressions
String message = switch (grade) {
case 'A', 'B' -> "Amazing";
case 'C' -> "Good";
default -> {
System.out.println("Oh NO !!!");
yield "Bad";
}
};
// for loop
for (int i = 0; i < arr.length; i++) {
...
}
// while loop
while (i < arr.length) {
...
}
// do-while loop
do {
...
} while (keepRunning);