Java Cheatsheet
Print Line Conditionals
[Link]("Hello, World!");
if (temp < 40) {
[Link]("Stay warm!");
} else if (temp < 80) {
Program Structure [Link]("Nice weather!");
} else {
[Link]("Go cool down!");
public class HelloWorld {
}
public static void main(String[] args) {
[Link]("Hello, World!");
}
Logical Operators
}
a && b // true if both are true
a || b // true if one is true
Comments
// Single line comments look like this
Relational Operators
/* Multi-line comments look
a == b // equal to
something like this */ a != b // not equal to
a > b // greater than
a < b // less than
Variables & Data Types a >= b // greater than or equal to
a <= b // less than or equal to
int luckyNumber = 7;
double pi = 3.14;
boolean sleptEightHours = false;
While Loop
char leastUsedLetter = 'z';
String cafeOrder = "chai"; while (pizzaSlicesEaten <= 5) {
pizzaSlicesEaten++;
}
Arithmetic Operators
total = 8 + 2; // total is now 10
For Loop
total = 8 - 2; // total is now 6
total = 8 * 2; // total is now 16
for (int i = 0; i < 5; i++) {
total = 8 / 2; // total is now 4
[Link](i);
total = 8 % 2; // total is now 0 }
User Input String Concatenation
import [Link];
[Link]("The tip is " + i);
public class Greetings {
public static void main(String[] args) {
Break
Scanner scan = new Scanner([Link]);
for (int i = 1; i <= 5; i++) {
[Link]("Enter your name: ");
if (i == 3) {
String name = [Link]();
break; // Exits the loop
}
}
}
}
Codédex