■ Java Cheat Sheet
1. Data Types
int → whole numbers (int x = 5;)
double → decimals (double pi = 3.14;)
char → one character (char letter = 'A';)
String → text (String name = "Aneeba";)
boolean → true/false (boolean flag = true;)
2. Operators
Arithmetic: +, -, *, /, %
Relational: ==, !=, <, <=, >, >=
Logical: && (and), || (or), ! (not)
3. If/Else Template
if (condition) {
// do this
} else if (anotherCondition) {
// do this instead
} else {
// fallback
}
4. Loops
For loop:
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
While loop:
while (condition) {
// keep running
}
Do-While loop:
do {
// run once
} while (condition);
5. Functions (Methods)
public static int doubleIt(int x) {
return x * 2;
}
Call: int result = doubleIt(5); // 10
6. Arrays
int[] numbers = {10, 20, 30};
System.out.println(numbers[0]); // 10
numbers[2] = 99; // change value
7. Input/Output
import java.util.Scanner;
Scanner sc = new Scanner(System.in);
System.out.print("Enter name: ");
String name = sc.nextLine();
System.out.println("Hello " + name);
8. Common Patterns
Sum of array:
int sum = 0;
for (int i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
Find max:
int max = numbers[0];
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] > max) {
max = numbers[i];
}
}
■ Exam Tips
Always end statements with ;
Curly braces {} are mandatory for blocks
System.out.println() for output
== for comparing values, .equals() for Strings