Java

OOP, collections, exceptions, multithreading

Practice →

Java is a platform-independent, object-oriented programming language known for its 'write once, run anywhere' capability. It powers enterprise applications, Android development, and web services. Understanding Java's core concepts-from OOP principles to advanced features like generics and concurrency-is essential for technical interviews and competitive programming.

Complexity / Key Facts

String.length() returns the number of characters\text{String.length() returns the number of characters}
Array length: arr.length (property, not method)\text{Array length: arr.length (property, not method)}
ArrayList size: list.size()\text{ArrayList size: list.size()}
For loop: for (int i = 0; i < n; i++)\text{For loop: for (int i = 0; i < n; i++)}
Enhanced for: for (Type item : collection)\text{Enhanced for: for (Type item : collection)}
Math.max(a, b), Math.min(a, b), Math.sqrt(x)\text{Math.max(a, b), Math.min(a, b), Math.sqrt(x)}
Integer.parseInt(String), String.valueOf(int)\text{Integer.parseInt(String), String.valueOf(int)}

Key Concepts

Object-Oriented Programming (OOP)

Java follows four OOP pillars:

1. Encapsulation: Bundling data and methods, controlling access via private fields with public getters/setters.

2. Inheritance: Creating new classes from existing ones using 'extends', promoting code reuse.

3. Polymorphism: Same method name, different behaviors-achieved through method overloading (compile-time) and overriding (runtime).

4. Abstraction: Hiding implementation details via abstract classes and interfaces.

Inheritance and Access Modifiers

Java supports single inheritance (one parent class). Access modifiers control visibility:

public: Accessible everywhere
private: Only within the class
protected: Within package and subclasses
default (no modifier): Within package only

The 'super' keyword calls parent class methods/constructors. 'this' refers to current instance.

Exception Handling

Java uses try-catch-finally blocks for error handling:

Checked exceptions: Must be handled (IOException, SQLException)
Unchecked exceptions: Runtime errors (NullPointerException, ArrayIndexOutOfBoundsException)

Syntax: try { riskyCode } catch (ExceptionType e) { handle } finally { always executes }

Custom exceptions extend Exception or RuntimeException.

Collections Framework

Core interfaces and implementations:

List: ArrayList (dynamic array), LinkedList (doubly-linked), Vector (thread-safe)
Set: HashSet (unique elements), TreeSet (sorted), LinkedHashSet (insertion order)
Map: HashMap (key-value), TreeMap (sorted by key), LinkedHashMap

ArrayList offers O(1) get but O(n) insert/delete middle. HashMap provides O(1) average for put/get.

Generics

Generics enable type-safe collections and methods:

Class<T>: Type parameter for generic class
<?>: Unbounded wildcard
<? extends T>: Upper bounded (T or subclasses)
<? super T>: Lower bounded (T or superclasses)

Example: ArrayList<String> list = new ArrayList<>(); prevents adding non-String objects at compile time.

Threads and Concurrency

Java provides multithreading via:

Extending Thread class
Implementing Runnable interface (preferred)

Key methods: start(), run(), sleep(ms), join(), yield()

Synchronization prevents race conditions:
synchronized methods
synchronized blocks
ReentrantLock for finer control

The volatile keyword ensures visibility of changes across threads.

Tips

  • Use .equals() for String content comparison, never == (except String literals).
  • Prefer ArrayList over Vector unless thread safety is required--Vector methods are synchronized and slower.
  • String is immutable; use StringBuilder for string concatenation in loops to avoid creating many objects.
  • Always handle checked exceptions or declare with throws; unchecked exceptions don't require handling.
  • For thread safety, consider ConcurrentHashMap over synchronized HashMap for better performance.
  • The finally block executes even if return statement is in try/catch--use for cleanup code.

Practice with questions

Real placement-style technical questions.

Start Exercise →