Blog/Java/Java Advanced

Java Advanced

10 essential java advanced interview questions

Reflection: ability to inspect and modify program at runtime. Get class info: Class.forName(), getClass(). Access methods, fields, constructors dynamically. Invoke methods: method.invoke(object). Use for frameworks, plugins, serialization. Performance cost: slower than direct access.
@interface CustomAnnotation { } creates annotation. Retention: when annotation available. Target: where applicable. Parameters: define annotation properties. Built-in: @Override, @Deprecated, @FunctionalInterface. Processing: reflection at runtime or compile-time processors.
Generics: type-safe collections and methods. <T> is type parameter. List<String> vs List (raw type). Benefits: type safety, eliminate casts, catch errors early. Wildcards: List<?>, List<? extends Number>. Avoid raw types. Enables code reuse across types.
Lambda: anonymous function for functional programming. Syntax: (params) -> body. Requires functional interface (one abstract method). Built-in: Runnable, Callable, Function, Predicate, Consumer. Reduces boilerplate. Works with streams. Enables functional style in Java.
Streams: sequence of elements supporting functional operations. Create: collection.stream(). Operations: map, filter, reduce, forEach. Lazy evaluation: operations don't execute until terminal operation. Parallel streams for multi-threaded processing. Great for data transformation.
Optional<T>: container for value or empty. Prevents NullPointerException. Methods: isPresent(), get(), orElse(default), ifPresent(action), map(), filter(). Preferred over null checks. Improves code readability. Signals to caller value might be absent.
Java 9+: modules provide explicit dependencies and encapsulation. Module-info.java defines exports and requires. Benefits: strong encapsulation, clear dependencies, faster startup. Challenges: complex setup, breaking changes. Optional to use.
Source code (.java) compiled to bytecode (.class). Bytecode is platform-independent. JVM interprets/JIT compiles bytecode to machine code. Allows write-once-run-anywhere. Tools: javac (compiler), javap (decompiler). Understanding bytecode helps debug.
java.util.concurrent provides thread-safe utilities: ExecutorService (thread pools), Future (async result), ConcurrentHashMap (thread-safe map), CountDownLatch, Semaphore (synchronization), CyclicBarrier. Prefer over manual synchronization. Essential for multi-threaded apps.
Tuning parameters: -Xmx (max heap), -Xms (initial heap), -XX:NewSize. GC tuning: choosing GC algorithm, pause time targets. JIT compilation: tiered compilation, code cache size. Profiling: JProfiler, YourKit. Monitoring: JMX, monitoring tools. Understanding JVM helps optimize performance.