Java Cheat Sheet

Data Types

Primitive Types

int age = 25;
double price = 9.99;
char grade = 'A';
boolean isActive = true;

Variables and Constants

int number = 10;
final double PI = 3.14159; // constant

Operators

Arithmetic

int sum = 5 + 3;
int product = 5 * 2;

Relational

boolean isEqual = (a == b);

Logical

boolean result = (x > 5) && (y < 10);

Type Casting

int i = (int) 3.14; // explicit
float f = 10; // implicit

Input / Output

Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
System.out.println("Hello " + name);

Comments

// Single line comment
/* Multi-line comment */

Control Flow

if / else / switch

if (x > 0) {
  System.out.println("Positive");
} else {
  System.out.println("Non-positive");
}

switch (day) {
  case 1: System.out.println("Mon"); break;
}

Loops

for (int i = 0; i < 5; i++) {}
while (x > 0) {}
do {} while (x > 0);

break / continue

for (int i = 0; i < 10; i++) {
  if (i == 5) continue;
  if (i == 8) break;
}

Object-Oriented Programming

Classes and Objects

class Dog {
  String name;
  void bark() {
    System.out.println("Woof");
  }
}
Dog d = new Dog();
d.name = "Rex";

Constructors, this, Overloading

Dog(String name) {
  this.name = name;
}
Dog(String name, int age) { ... }

Access Modifiers, Encapsulation

private int id;
public void setId(int id) { this.id = id; }

Inheritance, Overriding, Polymorphism

class Animal {}
class Cat extends Animal {
  @Override void speak() {}
}

Abstract Classes, Interfaces

abstract class Shape { abstract void draw(); }
interface Drawable { void draw(); }

Static, final, Inner Classes

static int count;
final int MAX = 100;
class Outer { class Inner {} }

Exception Handling

try {
  int result = 10 / 0;
} catch (ArithmeticException e) {
  System.out.println("Error: " + e.getMessage());
} finally {
  System.out.println("Cleanup");
}

void risky() throws IOException {}
throw new IllegalArgumentException("Bad input");

Collections and Data Structures

int[] nums = {1, 2, 3};
ArrayList list = new ArrayList<>();
LinkedList ll = new LinkedList<>();
HashMap map = new HashMap<>();
HashSet set = new HashSet<>();
Stack stack = new Stack<>();
Queue queue = new LinkedList<>();
Collections.sort(list);

Strings

String s = "hello";
s.length();
s.toUpperCase();
s.substring(1,3);

StringBuilder sb = new StringBuilder("hi");
sb.append(" there");

String a = "test";
String b = new String("test");
a == b // false
a.equals(b) // true

Advanced Topics

enum Color { RED, GREEN, BLUE; }

class Box { T value; }

Runnable r = () -> System.out.println("Hello");

List list = Arrays.asList("a", "b", "c");
list.stream().filter(x -> x.equals("a"));

@FunctionalInterface
interface MyFunc { void run(); }

@interface MyAnnotation {}

import java.util.*;
void printAll(String... args) {}

Multithreading

class MyThread extends Thread {
  public void run() {
    System.out.println("Running");
  }
}

Thread t = new MyThread();
t.start();

Runnable r = () -> {};
new Thread(r).start();

synchronized void syncMethod() {}

Thread.sleep(1000);
t.join();
wait(); notify();

File Handling

FileReader fr = new FileReader("file.txt");
BufferedReader br = new BufferedReader(fr);

FileWriter fw = new FileWriter("out.txt");
BufferedWriter bw = new BufferedWriter(fw);

ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("data.obj"));
oos.writeObject(obj);

Java Standard Libraries

// java.util: Collections, Date, Scanner
// java.lang: Math, String, Object (auto-imported)
// java.io: File, BufferedReader, InputStream

Miscellaneous

// JVM: Executes bytecode
// JRE: JVM + standard libraries
// JDK: JRE + development tools

public static void main(String[] args) {
  System.out.println("Hello Java");
}

for (String arg : args) {
  System.out.println(arg);
}

System.gc();

// Access JavaDoc: use javadoc tool or IDE shortcut
Mallikarjuna Mallisetty, author
About the author

Mallikarjuna Mallisetty

General programming

Mallikarjuna shares practical programming tutorials and foundational concepts designed to help developers learn by building and experimenting.

View LinkedIn profile ↗