Java Concept Summary — Part 1
For LeetCode-style Java problem solving (theory & keywords)
Contents
1. Java Basics & Syntax
2. Primitive types & Variables
3. Control Flow (if, switch, loops)
4. Methods & Parameter Passing
5. Classes & Objects (OOP)
6. Inheritance, Polymorphism, Abstraction, Encapsulation
7. Common Java Keywords (public, static, final, this, super, etc.)
8. Exception Handling
9. Collections Overview (List, Set, Map, Queue, Deque)
10. Arrays vs Collections
11. Generics
12. Inner classes & Anonymous classes
13. Lambda expressions & Streams (brief)
14. Concurrency basics (Threads, synchronized, Executors)
15. Memory model & Garbage collection (brief)
16. Input/Output basics (Files, Streams, Readers/Writers)
17. Common Data Structures in Java (LinkedList, ArrayList, HashMap, TreeMap, PriorityQueue)
18. Complexity Analysis (Big-O)
19. Common patterns for coding interviews (Two pointers, Sliding window, DFS/BFS, Backtracking,
DP)
20. Debugging tips, common pitfalls & best practices
21. Quick reference: common methods & snippets
1. Java Basics & Syntax
- A Java program is organized into classes. Execution starts at public static void
main(String[] args).
- File name must match public class name (e.g., public class Main in [Link]).
- Example:
public class Main {
public static void main(String[] args) {
[Link]("Hello");
}
}
2. Primitive types & Variables
- Primitives: byte (8-bit), short (16), int (32), long (64), float (32-bit float), double
(64-bit), char (16-bit UTF-16), boolean.
- Default values (fields): int -> 0, boolean -> false, object refs -> null.
- Literal suffixes: 123L for long, 1.2f for float.
- Casting:
int a = (int) 3.14; // truncates
- Wrapper types: Integer, Long, Double, Character, Boolean.
- Autoboxing/unboxing: Integer x = 5; int y = x + 1;
3. Control Flow
- if / else / else if
- switch (since Java 14 has enhanced switch expressions but classic form is common)
- Loops: for, enhanced for (for-each), while, do-while.
- Example:
for (int i = 0; i < n; i++) { ... }
for (String s : list) { ... }
4. Methods & Parameter Passing
- Signature: [modifiers] returnType name(params) { ... }
- Java is pass-by-value: for primitives value copied; for object references the reference is
copied (method sees same object but cannot reassign caller's reference).
- Example:
public int sum(int a, int b) { return a + b; }
5. Classes & Objects (OOP)
- Class: blueprint. Object: runtime instance.
- Fields (instance variables), methods, constructors.
- Constructor: same name as class; can be overloaded.
- Example:
public class Node {
int val;
Node next;
public Node(int v) { val = v; }
}
6. Inheritance, Polymorphism, Abstraction, Encapsulation
- Inheritance: class B extends A { }
- Polymorphism: a variable of type A can reference subclass B; method overriding decides
runtime behavior.
- Abstract class vs interface:
- abstract class may have implemented methods and fields.
- interface (since Java 8) can have default and static methods.
- Encapsulation: use private fields + public getters/setters.
7. Important Java Keywords (quick)
- public, private, protected — access modifiers
- static — belongs to class, not instance
- final — variable cannot be reassigned; method cannot be overridden; class cannot be extended
- this — reference to current object
- super — reference to parent class
- new — create instance
- return, break, continue
- synchronized, volatile (concurrency)
- transient, native, strictfp (less common in interviews)
8. Exception Handling
- Checked vs unchecked exceptions (checked must be declared or caught).
- try-catch-finally, try-with-resources (since Java 7).
- Common classes: Exception, RuntimeException, IOException, IllegalArgumentException,
NullPointerException.
- Example:
try (BufferedReader br = new BufferedReader(new FileReader("f"))) {
String s = [Link]();
} catch (IOException e) {
[Link]();
}
9. Collections Overview
- [Link] hierarchy:
- List (ordered, allows duplicates): ArrayList, LinkedList
- Set (unique elements): HashSet, LinkedHashSet, TreeSet (sorted)
- Queue / Deque: ArrayDeque, LinkedList, PriorityQueue
- Map: HashMap, LinkedHashMap, TreeMap (sorted)
- Important operations: add, remove, contains, get (for List/Map), put/get for Map.
- Iteration:
for (Type x : collection) { ... }
Iterator<Type> it = [Link](); while ([Link]()) { ... }
10. Arrays vs Collections
- Arrays: fixed-size, faster indexing, primitive arrays can be used.
- Collections: dynamic size, more utilities.
- Convert: [Link](...), [Link](new Type[0])
11. Generics (brief)
- Use parameterized types to avoid casts and ensure type safety.
List<String> list = new ArrayList<>();
- Wildcards: <?>, <? extends T>, <? super T>
- Type erasure: generic type info removed at runtime — can't do new T[] or instanceof
List<String>.
12. Inner classes & Anonymous classes
- Nested static class: static class Node { }
- Inner (non-static) class references outer instance.
- Anonymous class: new Runnable() { public void run() { ... } }
- Lambda expressions (since Java 8): (x) -> x * 2 for functional interfaces.
13. Lambda expressions & Streams (brief)
- Lambdas provide concise functions for single-method interfaces.
- Streams: pipeline operations (map, filter, collect). Example:
[Link]().filter(x -> x>0).map(x->x*2).collect([Link]());
- Useful for concise transformations, but avoid overusing in interviews unless comfortable.
14. Concurrency basics
- Thread: extend Thread or implement Runnable; better: ExecutorService pool.
- synchronized keyword for mutual exclusion on an object monitor.
- Locks: ReentrantLock, ReadWriteLock.
- volatile: ensures visibility of changes across threads (no atomicity).
- Executors:
ExecutorService ex = [Link](4);
[Link](() -> { /* task */ });
15. Memory model & Garbage collection (brief)
- Heap: objects; Stack: local variables and method frames.
- GC reclaims unreachable objects. Don't rely on finalize().
- Common GC roots: static fields, local variables on stack, JNI references.
16. Input/Output basics
- [Link] and [Link] packages.
- Readers/Writers for text; InputStream/OutputStream for bytes.
- BufferedReader, FileInputStream, FileReader, Files utility (since Java 7+).
- Example reading lines:
List<String> lines = [Link]([Link]("[Link]"));
17. Common Data Structures in Java (practical notes)
- ArrayList: dynamic array; get O(1), add amortized O(1), remove O(n).
- LinkedList: good for frequent inserts/removes at ends; random access O(n).
- HashMap: average O(1) put/get; keys must have proper equals() and hashCode().
- TreeMap: O(log n) operations, sorted keys.
- PriorityQueue: min-heap by default; use custom comparator for max-heap.
18. Complexity Analysis (Big-O)
- Know O(1), O(log n), O(n), O(n log n), O(n^2), O(2^n), O(n!) and how common algorithms map to
them.
- Space complexity: extra memory used.
- Typical examples: sorting O(n log n), BFS/DFS O(V+E), HashMap operations average O(1).
19. Common patterns for coding interviews
- Two pointers: left/right indices move towards each other — useful for sorted arrays,
container problems.
- Sliding window: variable-size window for substring/subarray problems (min window, longest
substring).
- Fast & slow pointers: cycle detection, middle of linked list.
- DFS/BFS: tree/graph traversal; recursion/backtracking often DFS.
- Backtracking: generate combinations/permutations with pruning.
- Dynamic Programming: identify overlapping subproblems & optimal substructure; memoization or
tabulation.
- Greedy: choose local optimum; verify correctness.
20. Debugging tips, common pitfalls & best practices
- NullPointerException: check for null before dereferencing, use Optional when helpful.
- Off-by-one errors: careful with indices and boundaries.
- Mutating collections while iterating: use [Link]() or collect-to-remove.
- For maps: avoid modifying keys' mutable fields used in hashCode/equals.
- Prefer interfaces in signatures (List instead of ArrayList).
- Keep methods small, single responsibility, meaningful names.
- Write small test cases, e.g., edge cases: empty, single element, duplicated elements,
extremes.
21. Quick reference: common snippets
- Reverse a linked list:
ListNode prev = null;
while(head!=null) {
ListNode next = [Link];
[Link] = prev;
prev = head; head = next;
}
- Binary search template (iterative):
int l=0, r=n-1;
while(l<=r) {
int m = l + (r-l)/2;
if(a[m]==target) return m;
if(a[m]<target) l=m+1; else r=m-1;
}
- DFS recursive:
void dfs(Node node) {
if(node==null) return;
visit(node);
dfs([Link]); dfs([Link]);
}
- BFS with queue:
Queue<Node> q = new ArrayDeque<>();
[Link](root);
while(![Link]()) {
Node n = [Link]();
if([Link]!=null) [Link]([Link]);
if([Link]!=null) [Link]([Link]);
}
Appendix: Suggested Study Plan for 2 days offline
Day 1:
- Quick read: Sections 1-11 (syntax, methods, OOP, collections).
- Practice 10 array/string/easy problems applying basic concepts.
Day 2:
- Read Sections 12-21 (exceptions, generics, concurrency, patterns).
- Practice 15-20 mixed problems focusing on patterns (two pointers, sliding window, DFS/BFS).
- Review tricky Java keywords and snippets.
Final notes
- The PDF focuses on concise explanations and practical snippets you will see in LeetCode Java
solutions.
- If you'd like, I can also attach a one-page cheat-sheet with common Java methods (String,
Arrays, Collections) and common interview pitfalls.