youtube java refference imp Q&A
Functional interface.
→ A functional interface in Java is an interface that contains exactly one abstract method, making it ideal for use with lambda expressions and method references.
→ A functional interface in Java cannot have two abstract methods.
→You can still have:
Default methods
Static methods
But only one abstract method is allowed.
Optional class:
→
Optionalis a container object introduced in Java 8 to represent nullable values.→It helps avoid
NullPointerExceptionby providing safe access to potentially missing data.→ Common methods:
isPresent()– checks if a value is present.get()– retrieves the value (unsafe if empty).orElse(default)– returns value or default.ifPresent(Consumer)– executes logic if a value exists.
Map and flatmap:
→
map()is for one-to-one transformations, whileflatMap()is for one-to-many and flattening nested streams."How is Java Time api better than the old date calendar APIs:
→ It offers better parsing, formatting, and time zone handling with classes like
LocalDate,ZonedDateTime, andDateTimeFormatter.→The Java Time API is immutable, thread-safe, and provides a cleaner, more intuitive design than the old
DateandCalendarclasses.What makes class immutable?
→A class is considered immutable in Java when its state (i.e., its fields) cannot be changed after it's created. Here's what makes a class immutable:
✅ Key Rules for Immutability
Make the class
final– prevents subclassing, which could alter behavior.Make all fields
privateandfinal– ensures they’re set once and never changed.No setters – only getters to expose data.
Initialize fields via the constructor only.
Avoid exposing mutable objects directly – return copies or use defensive cloning.
If a class contains date, how to ensure immutablity?
→Since
Dateis mutable, we ensure immutability by making defensive copies in the constructor and getter. This prevents external code from modifying the internal state."What are intermediate operations and terminal operations?
→Intermediate operations in Java Streams are lazy and return another stream, like
filterormap. While Terminal operations trigger execution and produce the final result, likecollect,count, orforEach.What are design patterns
→ Design patterns are proven, reusable solutions to common software design problems. They help developers write cleaner, more maintainable, and scalable code by following best practices.
What happens if we use request param and path param in the same method?
→ Use PathVariable for resource identification (e.g., user ID, product ID).
→ Use RequestParam for filters, sorting, and optional flags (e.g., active status, page number, search keyword).
Can we have multiple controller advice?
→Allowed: Spring supports multiple
@ControllerAdviceclasses. Each one can handle exceptions, bind data, or apply advice to specific controllers.What are the different thread states?
→In Java, a thread can exist in six different states: NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, and TERMINATED
→A thread begins in NEW, then moves to RUNNABLE when started.
→While running, it may pause in WAITING, TIMED_WAITING, or get BLOCKED by locks.
→Once finished, it goes to TERMINATED and cannot be restarted.
What are the significant in HashMap?
→HashMap is significant because it provides fast lookups and updates using hashing, supports null keys/values, and is widely used for storing key-value pairs in applications like caching, configuration, and quick data retrieval
When should I go for HashMap, and when should I go for ConcurrentHashMap?
→I use
HashMapIn single-threaded scenarios where performance is critical, and thread safety isn’t needed. For multi-threaded environments, I preferConcurrentHashMapbecause it provides built-in thread safety with better concurrency than synchronized maps, ensuring safe reads and writes without blocking the entire map.How does Microservice 1 talk to Microservice 2?
→ Microservices communicate either synchronously using REST/gRPC or asynchronously using message brokers like Kafka. For dynamic environments, we use service discovery and API gateways to manage communication.
What is a circuit breaker pattern?
→ The Circuit Breaker Pattern is used in microservices to stop calls to a service that is failing repeatedly. It works in three states — closed, open, and half-open — to protect the system from cascading failures and allow recovery.
Internal working of HashMap?
A HashMap in Java stores data in key–value pairs using an internal array of buckets. Each bucket holds a linked list or tree of nodes. Keys are converted to a hash code, mapped to a bucket index, and collisions are resolved using chaining or tree structures
What is a Unique Constraint in a database?
→ A unique constraint makes sure that the values in a column are not repeated. It allows only unique values. Unlike a primary key, you can have many unique constraints in a table, and they can allow NULL values.
What is a primary key in a database?
→A primary key uniquely identifies each record in a table, ensuring data integrity by enforcing uniqueness and non-null constraints.
What is a foreign key in a database?
→A foreign key is used to establish relationships between tables and maintain referential integrity, ensuring that child records always point to valid parent records.
What is normalization?
→Normalization is used to structure a database by removing redundancy and ensuring data integrity. It’s achieved through normal forms like 1NF, 2NF, and 3NF.
Why do we use an index in a database?
→We use indexes to improve query performance by allowing the database to quickly locate rows without scanning the entire table. They’re essential for large datasets, though they add overhead during write operations.