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
Post a Comment