All Cheat Sheets
Java
Java Cheat Sheet
Basics
Hello WorldBasic Java programpublic class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}Data typesPrimitive typesint age = 25;
double price = 99.99;
char grade = 'A';
boolean isActive = true;VariablesVariable declarationString name = "Mishu";
final int MAX = 100;Control Flow
If-elseConditional executionif (score >= 90) {
System.out.println("A");
} else if (score >= 80) {
System.out.println("B");
} else {
System.out.println("C");
}For loopIterationfor (int i = 0; i < 10; i++) {
System.out.println(i);
}While loopCondition-based loopwhile (x > 0) {
x--;
}Enhanced forIterate over arrays/collectionsfor (String item : items) {
System.out.println(item);
}OOP
ClassBlueprint for objectspublic class Car {
private String model;
public Car(String model) {
this.model = model;
}
public void drive() {
System.out.println(model + " is driving");
}
}InheritanceExtend a classpublic class ElectricCar extends Car {
public ElectricCar(String model) {
super(model);
}
}InterfaceContract for classesinterface Drawable {
void draw();
}
class Circle implements Drawable {
public void draw() { ... }
}ArrayFixed-size collectionint[] numbers = {1, 2, 3};
int first = numbers[0];
numbers.length;