Java Cheatsheet

☕ Java Basics

// Hello World
public class HelloWorld {
  public static void main(String[] args) {
    System.out.println("Hello, World!");
  }
}

💡 Tip: Every Java program starts with a main method inside a class.

📦 Variables & Data Types

int age = 25;
double price = 99.99;
char grade = 'A';
boolean isActive = true;
String name = "Maxon";

💡 Tip: Use wrapper classes (Integer, Double, etc.) for objects and generics.

🔀 Control Flow

int num = 10;

// if-else
if (num > 5) {
  System.out.println("Greater than 5");
} else {
  System.out.println("Less or equal to 5");
}

// switch
switch (num) {
  case 5: System.out.println("Five"); break;
  case 10: System.out.println("Ten"); break;
  default: System.out.println("Other");
}

💡 Tip: Use switch for cleaner multi-condition checks.

🔁 Loops

// for loop
for (int i = 0; i < 5; i++) {
  System.out.println(i);
}

// while loop
int j = 0;
while (j < 5) {
  System.out.println(j);
  j++;
}

💡 Tip: Use enhanced for-each for arrays & collections.

🏗️ OOP (Classes & Objects)

class Person {
  String name;
  int age;

  // Constructor
  Person(String n, int a) {
    name = n;
    age = a;
  }

  // Method
  void greet() {
    System.out.println("Hi, I'm " + name);
  }
}

public class Main {
  public static void main(String[] args) {
    Person p1 = new Person("Maxon", 25);
    p1.greet();
  }
}

💡 Tip: Follow OOP principles: Encapsulation, Inheritance, Polymorphism, Abstraction.

🧬 Inheritance

class Animal {
  void sound() { System.out.println("Animal sound"); }
}

class Dog extends Animal {
  void sound() { System.out.println("Bark"); }
}

public class Main {
  public static void main(String[] args) {
    Animal a = new Dog();
    a.sound(); // Bark
  }
}

💡 Tip: Use @Override to explicitly override methods.

🔗 Interfaces

interface Vehicle {
  void drive();
}

class Car implements Vehicle {
  public void drive() {
    System.out.println("Car driving...");
  }
}

public class Main {
  public static void main(String[] args) {
    Vehicle v = new Car();
    v.drive();
  }
}

💡 Tip: Use interfaces for multiple inheritance and abstraction.

📚 Collections

import java.util.*;

public class Main {
  public static void main(String[] args) {
    List list = new ArrayList<>();
    list.add("Apple");
    list.add("Banana");
    System.out.println(list);

    Set set = new HashSet<>();
    set.add(1); set.add(2); set.add(1); // Duplicates ignored
    System.out.println(set);

    Map map = new HashMap<>();
    map.put("A", 1);
    map.put("B", 2);
    System.out.println(map.get("A"));
  }
}

💡 Tip: Prefer generics (List<String>) for type safety.

🚨 Exception Handling

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

💡 Tip: Create custom exceptions by extending Exception.

⚡ Multithreading

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

public class Main {
  public static void main(String[] args) {
    MyThread t1 = new MyThread();
    t1.start();
  }
}

💡 Tip: Use Runnable or ExecutorService for better thread management.

🌊 Java Streams API

import java.util.*;

public class Main {
  public static void main(String[] args) {
    List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

    numbers.stream()
      .filter(n -> n % 2 == 0)
      .map(n -> n * n)
      .forEach(System.out::println);
  }
}

💡 Tip: Streams provide functional-style operations on collections.

⚙️ Lambda Expressions

import java.util.*;

public class Main {
  public static void main(String[] args) {
    List<String> list = Arrays.asList("A", "B", "C");

    list.forEach(item -> System.out.println(item));
  }
}

💡 Tip: Use lambdas for cleaner and concise code (Java 8+).

Post a Comment