Skip to main content

Posts

Java Interview Question: Comparator vs Comparable in Java (With Examples & Differences)

  🔹 Introduction Sorting objects is a common requirement in Java, and interviewers often ask: 👉 “What is the difference between Comparator and Comparable?” 🔹 What is Comparable? Comparable is used to define natural sorting . 👉 It is implemented inside the class itself 🔹 Example (Comparable) import java . util . *; class Employee implements Comparable < Employee > { int salary ; Employee ( int salary ) { this . salary = salary ; } public int compareTo ( Employee e ) { return this . salary - e . salary ; } } public class Test { public static void main ( String [] args ) { List < Employee > list = Arrays . asList ( new Employee ( 50000 ), new Employee ( 30000 ), new Employee ( 70000 ) ); Collections . sort ( list ); list . forEach ( e -> System . out . println ( e . salary )); } } 🔹 What is Comparator? Comparator i...
Recent posts

Java Interview Question: What is ExecutorService in Java? Thread Pool Explained (With Examples)

  🔹 Introduction Managing threads manually in Java can be complex and inefficient. 👉 Interviewers often ask: “What is ExecutorService and why is it used?” In this article, we will understand: Thread pools ExecutorService Real-world usage 🔹 What is ExecutorService? ExecutorService is a framework in Java used to manage threads efficiently. 👉 It provides: Thread pooling Task scheduling Better resource management 🔹 Why Not Create Threads Manually? ❌ Creating threads manually: High overhead Difficult to manage Poor performance 👉 Solution: Thread Pool using ExecutorService 🔹 Creating ExecutorService import java . util . concurrent . *; public class Test { public static void main ( String [] args ) { ExecutorService executor = Executors . newFixedThreadPool ( 3 ); for ( int i = 1 ; i <= 5 ; i ++ ) { int task = i ; executor . execute (() -> { System . out . pr...

Java Interview Question: What is Optional in Java 8? How to Avoid NullPointerException (With Examples)

 🔹 Introduction Handling null values is one of the biggest challenges in Java. 👉 A very common interview question is: “What is Optional in Java and how does it help avoid NullPointerException?” 🔹 What is Optional? Optional is a container object introduced in Java 8. 👉 It is used to: Represent a value that may or may not be present Avoid NullPointerException 🔹 Why Do We Need Optional? Before Java 8: String name = null ; System . out . println ( name . length ()); // NullPointerException 👉 This causes runtime error ❌ 🔹 Using Optional import java . util . Optional ; public class Test { public static void main ( String [] args ) { Optional < String > name = Optional . ofNullable ( null ); System . out . println ( name . orElse ( "Default Name" )); } } 🔹 Output Default Name 🔍 Important Methods of Optional 🔹 of() Optional < String > name = Optional . of ( "Java" ); 👉 Throws excep...

Java Interview Question: Why String is Immutable in Java (With Examples & Benefits)

 🔹 Introduction One of the most commonly asked Core Java interview questions is: 👉 “Why is String immutable in Java?” This question tests your understanding of: Memory management Security Performance 🔹 What Does Immutable Mean? An immutable object is one whose state cannot be changed after creation. 👉 In Java: Once a String is created, it cannot be modified 🔹 Example public class Test { public static void main ( String [] args ) { String str = "hello" ; str . concat ( " world" ); System . out . println ( str ); } } 🔹 Output hello 👉 Because original string is not modified 🔍 How String is Immutable Internally String class is final Internal value stored in char array No setter methods 👉 Any modification creates a new object 🔹 Example with New Object String str = "hello" ; str = str . concat ( " world" ); System . out . println ( str ); 👉 Output: he...

Java Interview Question: Difference Between == and equals() in Java (With Examples)

 🔹 Introduction Understanding the difference between == and equals() is one of the most fundamental Java interview questions. 👉 Interviewers ask this to check your understanding of: Memory Object comparison String handling 🔹 What is == in Java? == is used to compare memory references (addresses) . 👉 It checks: Whether two variables point to the same object 🔹 Example Using == public class Test { public static void main ( String [] args ) { String a = new String ( "hello" ); String b = new String ( "hello" ); System . out . println ( a == b ); } } 🔹 Output false 👉 Because both objects are stored in different memory locations 🔹 What is equals() in Java? equals() is used to compare actual content (values) . 🔹 Example Using equals() public class Test { public static void main ( String [] args ) { String a = new String ( "hello" ); String b...

Java Interview Question: What is Deadlock and How to Create It in Java (With Example & Prevention)

  🔹 Introduction Deadlock is one of the most critical concepts in Java multithreading and is frequently asked in interviews. 👉 Interviewers often ask: “What is deadlock? Can you create one in Java?” In this article, we will: Understand deadlock Create a deadlock Learn how to prevent it 🔹 What is Deadlock? A deadlock occurs when two or more threads are waiting for each other indefinitely, and none of them can proceed. 👉 Simple idea: Thread 1 holds Resource A and waits for B Thread 2 holds Resource B and waits for A 💥 Both get stuck forever 🖼️ Visualization (Understand Easily) Thread 1 → holds Lock A → waiting for Lock B Thread 2 → holds Lock B → waiting for Lock A Result: DEADLOCK 🔹 Java Code to Create Deadlock public class DeadlockExample { private static final Object lock1 = new Object (); private static final Object lock2 = new Object (); public static void main ( String [] args ) { Thread t1 = new ...

Java Interview Question: How HashMap Works Internally (With Diagram & Explanation)

 🔹 Introduction HashMap is one of the most important data structures in Java and is frequently asked in interviews. 👉 Interviewers often ask: “Explain how HashMap works internally?” In this article, we will break it down in a simple and easy-to-understand way. 🔹 What is HashMap? HashMap stores data in key-value pairs . Example: Map < String , Integer > map = new HashMap <>(); map . put ( "A" , 1 ); map . put ( "B" , 2 ); 🔍 Internal Working of HashMap Step-by-Step: Key is passed to hashCode() Hash is converted to index Value is stored in bucket (array) If collision occurs → handled using LinkedList / Tree 🖼️ Visualization (Very Important) Index Data (Bucket) 0 → null 1 → [A=1] → [F=6] 2 → null 3 → [B=2] 4 → [C=3] → [H=8] 👉 Same index → collision 👉 Stored as LinkedList (or Tree in Java 8+) 🔹 Java Code Example import java . util . *; public class HashMapExample { public static void...