cheat sheet java

 Here’s a complete Java cheat sheet for quick interview preparation. It’s organized by topic and designed for fast revision — ideal before interviews.





๐Ÿง  Java Interview Cheat Sheet




✅ Core Java Basics




Data Types



  • Primitive Types: byte, short, int, long, float, double, char, boolean
  • Wrapper Classes: Byte, Short, Integer, Long, Float, Double, Character, Boolean




Keywords



  • static, final, abstract, synchronized, volatile, transient, default, this, super, instanceof, enum




Access Modifiers


Modifier

Same Class

Same Package

Subclass

Everywhere

private

✔️

default

✔️

✔️

protected

✔️

✔️

✔️

public

✔️

✔️

✔️

✔️





๐Ÿ“ฆ OOP Concepts



  • Encapsulation: Data hiding via private fields + public getters/setters.
  • Abstraction: Using interfaces or abstract classes.
  • Inheritance: extends keyword; single inheritance only.
  • Polymorphism:
    • Compile-time (method overloading)
    • Runtime (method overriding)






๐Ÿงฑ Class Structure


public class MyClass {

    private int value;

    

    public MyClass(int val) {

        this.value = val;

    }


    public void print() {

        System.out.println(value);

    }

}





๐Ÿ” Control Statements




Loops


for (int i = 0; i < 10; i++) {}

while (condition) {}

do {} while (condition);


Conditions


if (x > 5) {}

else if (x == 5) {}

else {}


switch(day) {

  case "MONDAY": break;

  default: break;

}





๐Ÿ“š Collections Framework




List



  • ArrayList, LinkedList




Set



  • HashSet, LinkedHashSet, TreeSet




Map



  • HashMap, LinkedHashMap, TreeMap, Hashtable




Queue



  • PriorityQueue, ArrayDeque




Key Methods


list.add("x"); list.get(0); list.contains("x");

map.put("key", "val"); map.get("key");





๐ŸŽฏ Java 8 Features




Lambda Expressions


List<String> list = Arrays.asList("a", "b");

list.forEach(x -> System.out.println(x));


Streams API


list.stream().filter(x -> x.startsWith("a")).collect(Collectors.toList());


Functional Interfaces



  • Runnable, Callable, Predicate<T>, Function<T,R>, Consumer<T>




Default & Static Methods in Interfaces


interface A {

    default void print() {}

    static void show() {}

}


Optional


Optional<String> opt = Optional.of("Hello");

opt.ifPresent(System.out::println);





๐Ÿงต Multithreading




Creating Thread


Thread t = new Thread(() -> System.out.println("Running"));

t.start();


Synchronization


synchronized (this) {

    // thread-safe block

}


Executors


ExecutorService ex = Executors.newFixedThreadPool(2);

ex.submit(() -> System.out.println("task"));

ex.shutdown();





๐Ÿ—ƒ️ Exception Handling


try {

    // code

} catch (Exception e) {

    e.printStackTrace();

} finally {

    // always runs

}


Types:



  • Checked: IOException, SQLException
  • Unchecked: NullPointerException, ArrayIndexOutOfBounds






๐Ÿงฉ Interfaces & Abstract Classes




Interface


interface Vehicle {

    void start();

}


Abstract Class


abstract class Animal {

    abstract void sound();

}





๐ŸŒ String Handling




Common Methods


str.length(); str.charAt(0); str.substring(1);

str.contains("abc"); str.equals("abc"); str.trim();


StringBuilder


StringBuilder sb = new StringBuilder("Hello");

sb.append(" World");





๐Ÿ’พ File Handling (Java NIO)


Path path = Paths.get("file.txt");

Files.write(path, "data".getBytes());

List<String> lines = Files.readAllLines(path);





๐Ÿ“„ Java Memory Areas



  • Heap: Objects
  • Stack: Method calls, local variables
  • Method Area: Class-level metadata
  • GC: Automatically collects unreachable objects






๐Ÿšฆ Keywords Recap


Keyword

Use

final

Constant, prevents override

static

Shared among all instances

this

Refers to current object

super

Refers to parent class

transient

Skip during serialization

volatile

Guarantees visibility between threads

synchronized

Lock mechanism for thread safety





๐Ÿงช Miscellaneous



  • equals() vs ==: .equals() checks content; == checks reference.
  • hashCode(): Used in hashing (HashMap, HashSet).
  • Comparable vs Comparator: Natural vs custom sorting.
  • Serialization: implements Serializable, use ObjectOutputStream.






Comments