A simple Java interview question… that isn’t so simple 👇 class Main { public static void main(String[] args) { String name = "Srishti"; String b = new String("Srishti"); String c = name; String d = b; System.out.println(name == b); System.out.println(name == c); } } I was asked this in an interview and it’s a perfect example of how fundamentals matter more than complexity. Most people expect both outputs to be true. But the actual result is: false true 💡 Why? Because in Java: 🔹 "Srishti" is stored in the String Pool (optimized memory) 🔹 new String("Srishti") creates a new object in Heap 🔹 c = name → same reference 🔹 d = b → same reference ⚡ The key insight: == compares memory reference, not content 👉 name == b → false (different objects) 👉 name == c → true (same object) If you actually want to compare values: name.equals(b); // true 📌 What this question really tests: Not syntax. Not memorization. But your understanding of how Java handles memory. #Java #CodingInterview #SoftwareEngineering #Programming #Developers #TechCareers
Java Interview Question: String Pool vs Heap Memory
More Relevant Posts
-
A simple Java interview question 👇 class Main { public static void main(String[] args) { String name = "Hello"; String b = new String("Hello"); String c = name; String d = b; System.out.println(name == b); System.out.println(name == c); } } I was asked this in an interview and it’s a perfect example of how fundamentals matter more than complexity. Most people expect both outputs to be true. But the actual result is: false true 💡 Why? Because in Java: 🔹 "Hello" is stored in the String Pool (optimized memory) 🔹 new String("Hello") creates a new object in Heap 🔹 c = name → same reference 🔹 d = b → same reference ⚡ The key insight: == compares memory reference, not content 👉 name == b → false (different objects) 👉 name == c → true (same object) If you actually want to compare values: name.equals(b); // true 📌 What this question really tests: Not syntax. Not memorization. But your understanding of how Java handles memory. #Java #CodingInterview #SoftwareEngineering #Programming #Developers #TechCareers
To view or add a comment, sign in
-
If you're preparing for Java interviews, this is a must-know concept! 🔒 What is an Immutable Class in Java? Why & How to Create One? In Java, an Immutable Class is a class whose objects cannot be modified once they are created. 👉 The best example is the String class — once a string is created, its value cannot be changed. --- 💡 Why do we need Immutable Classes? ✔️ Thread-safe (no synchronization needed) ✔️ Secure (data cannot be changed accidentally) ✔️ Easy to cache and reuse ✔️ Predictable behavior (no side effects) --- 🛠️ How to Create an Immutable Class? Follow these 5 rules: 1. Make the class `final` (cannot be extended) 2. Make all fields `private final` 3. Do NOT provide setter methods 4. Initialize all fields through constructor 5. Return copies of mutable objects (defensive copying) --- 💻 Example: ``` final class Employee { private final String name; private final int age; public Employee(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge(){ return age; } // "Wither" pattern — returns a NEW object instead of mutating public Employee withAge(int newAge) { return new Employee(this.name, newAge); } } ``` --- 🚀 Key Takeaway: Immutable objects are simple, safe, and powerful — especially in multi-threaded applications. #Java #SpringBoot #Programming #SoftwareDevelopment #Coding #JavaDeveloper #InterviewPreparation
To view or add a comment, sign in
-
-
🚀 30 Days of Java Interview Questions – Day 28 💡 Question: What is Java Stream API and how does it work? 🔹 What is Stream API? Stream API is used to process collections of data in a functional and declarative way. It helps write cleaner and more readable code. --- 🔹 Key Features • Functional programming style • Declarative approach • Lazy evaluation • Supports parallel processing • Reduces boilerplate code --- 🔹 How it works Collection → Stream created → Intermediate operations (filter, map) → Terminal operation (collect, forEach) → Result --- 🔹 Example ```java id="s9k3d2" List<String> names = Arrays.asList("Java", "Python", "JavaScript", "C++"); List<String> result = names.stream() .filter(name -> name.startsWith("J")) .map(String::toUpperCase) .collect(Collectors.toList()); System.out.println(result); ``` --- 🔹 Common Operations • filter() • map() • sorted() • distinct() • count() • collect() --- ⚡ Quick Facts • Introduced in Java 8 • Works with collections and arrays • Improves performance and readability --- 📌 Interview Tip Use Streams when working with large datasets and complex transformations. --- Follow this series for 30 Days of Java Interview Questions. #java #javadeveloper #codinginterview #backenddeveloper #softwareengineer #programming #developers #tech
To view or add a comment, sign in
-
-
Basic stream API, means what is stream API and what is the benefits of using stream API aow we use stream API?
Software Engineer at Acutec Global Services | Java | Spring Boot & MVC | JPA | Hibernate | MySQL | Oracle DB | Spring Security | Ex- IDEMIA & Orage Technologies
🚀 30 Days of Java Interview Questions – Day 28 💡 Question: What is Java Stream API and how does it work? 🔹 What is Stream API? Stream API is used to process collections of data in a functional and declarative way. It helps write cleaner and more readable code. --- 🔹 Key Features • Functional programming style • Declarative approach • Lazy evaluation • Supports parallel processing • Reduces boilerplate code --- 🔹 How it works Collection → Stream created → Intermediate operations (filter, map) → Terminal operation (collect, forEach) → Result --- 🔹 Example ```java id="s9k3d2" List<String> names = Arrays.asList("Java", "Python", "JavaScript", "C++"); List<String> result = names.stream() .filter(name -> name.startsWith("J")) .map(String::toUpperCase) .collect(Collectors.toList()); System.out.println(result); ``` --- 🔹 Common Operations • filter() • map() • sorted() • distinct() • count() • collect() --- ⚡ Quick Facts • Introduced in Java 8 • Works with collections and arrays • Improves performance and readability --- 📌 Interview Tip Use Streams when working with large datasets and complex transformations. --- Follow this series for 30 Days of Java Interview Questions. #java #javadeveloper #codinginterview #backenddeveloper #softwareengineer #programming #developers #tech
To view or add a comment, sign in
-
-
Can we overload the "𝐦𝐚𝐢𝐧()" method in Java? This is a common interview question that many confuse during interview. Let’s break it down simply 📝 ➡️ Yes, we can overload "main()" In Java, method overloading means: ♣️Same method name, but different method parameters So, we can have multiple "main()" methods in the same class. 👨💻 Example 𝐜𝐥𝐚𝐬𝐬 𝐓𝐞𝐬𝐭 { 𝐩𝐮𝐛𝐥𝐢𝐜 𝐬𝐭𝐚𝐭𝐢𝐜 𝐯𝐨𝐢𝐝 𝐦𝐚𝐢𝐧(𝐒𝐭𝐫𝐢𝐧𝐠[] 𝐚𝐫𝐠𝐬) { 𝐒𝐲𝐬𝐭𝐞𝐦.𝐨𝐮𝐭.𝐩𝐫𝐢𝐧𝐭𝐥𝐧("𝐎𝐫𝐢𝐠𝐢𝐧𝐚𝐥 𝐦𝐚𝐢𝐧"); // 𝐜𝐚𝐥𝐥𝐢𝐧𝐠 𝐨𝐯𝐞𝐫𝐥𝐨𝐚𝐝𝐞𝐝 𝐦𝐞𝐭𝐡𝐨𝐝𝐬 𝐦𝐚𝐢𝐧(𝟏𝟎); 𝐦𝐚𝐢𝐧("𝐇𝐞𝐥𝐥𝐨"); } 𝐩𝐮𝐛𝐥𝐢𝐜 𝐬𝐭𝐚𝐭𝐢𝐜 𝐯𝐨𝐢𝐝 𝐦𝐚𝐢𝐧(𝐢𝐧𝐭 𝐚) { 𝐒𝐲𝐬𝐭𝐞𝐦.𝐨𝐮𝐭.𝐩𝐫𝐢𝐧𝐭𝐥𝐧("𝐎𝐯𝐞𝐫𝐥𝐨𝐚𝐝𝐞𝐝 𝐦𝐚𝐢𝐧 𝐰𝐢𝐭𝐡 𝐢𝐧𝐭: " + 𝐚); } 𝐩𝐮𝐛𝐥𝐢𝐜 𝐬𝐭𝐚𝐭𝐢𝐜 𝐯𝐨𝐢𝐝 𝐦𝐚𝐢𝐧(𝐒𝐭𝐫𝐢𝐧𝐠 𝐦𝐬𝐠) { 𝐒𝐲𝐬𝐭𝐞𝐦.𝐨𝐮𝐭.𝐩𝐫𝐢𝐧𝐭𝐥𝐧("𝐎𝐯𝐞𝐫𝐥𝐨𝐚𝐝𝐞𝐝 𝐦𝐚𝐢𝐧 𝐰𝐢𝐭𝐡 𝐒𝐭𝐫𝐢𝐧𝐠: " + 𝐦𝐬𝐠); } } 📑 𝐎𝐮𝐭𝐩𝐮𝐭 𝐎𝐫𝐢𝐠𝐢𝐧𝐚𝐥 𝐦𝐚𝐢𝐧 𝐎𝐯𝐞𝐫𝐥𝐨𝐚𝐝𝐞𝐝 𝐦𝐚𝐢𝐧 𝐰𝐢𝐭𝐡 𝐢𝐧𝐭: 𝟏𝟎 𝐎𝐯𝐞𝐫𝐥𝐨𝐚𝐝𝐞𝐝 𝐦𝐚𝐢𝐧 𝐰𝐢𝐭𝐡 𝐒𝐭𝐫𝐢𝐧𝐠: 𝐇𝐞𝐥𝐥𝐨 ⚠️ Important Point 🔍 JVM looks specifically for this method with (String[] args) as parameters to start execution: 𝐩𝐮𝐛𝐥𝐢𝐜 𝐬𝐭𝐚𝐭𝐢𝐜 𝐯𝐨𝐢𝐝 𝐦𝐚𝐢𝐧(𝐒𝐭𝐫𝐢𝐧𝐠[] 𝐚𝐫𝐠𝐬) ➡️ Here, String[] args represents command-line arguments ❌ Other overloaded "main()" methods are not called automatically ➡️ We need to call them explicitly (as shown above) 𝐖𝐞 𝐬𝐚𝐰 𝐨𝐯𝐞𝐫𝐥𝐨𝐚𝐝𝐢𝐧𝐠… 𝐛𝐮𝐭 𝐰𝐡𝐚𝐭 𝐚𝐛𝐨𝐮𝐭 𝐭𝐡𝐢𝐬? 🔍 𝐂𝐚𝐧 𝐰𝐞 𝐨𝐯𝐞𝐫𝐫𝐢𝐝𝐞 𝐭𝐡𝐞 𝐦𝐚𝐢𝐧() 𝐦𝐞𝐭𝐡𝐨𝐝 𝐢𝐧 𝐉𝐚𝐯𝐚? #Java #JavaDeveloper #JavaBackend #Programming #TechJourney #LearnBySharing #JavaConcepts #OOP #InterviewPrep #Coding
To view or add a comment, sign in
-
💡 Java Interview Question – Immutable Strings Trap What will be the output? String s = "Java"; s.replace("J", "K"); System.out.println(s); 🤔 Options: A) Kava B) Java C) Compilation Error D) Runtime Exception --- ✅ Correct Answer: B) Java --- 🔍 Explanation: In Java, String is immutable. That means once a String object is created, it cannot be modified. 👉 The "replace()" method does not change the original string, it returns a new modified string. But here’s the catch 👇 s.replace("J", "K"); We are not assigning the result back to "s", so the original value remains unchanged. --- 💡 Correct way to modify: s = s.replace("J", "K"); System.out.println(s); // Output: Kava --- 🚀 Key Takeaway: Always remember: 👉 Strings in Java are immutable → Reassignment is required for changes --- #Java #JavaInterview #Coding #BackendDevelopment #Programming #InterviewPrep
To view or add a comment, sign in
-
🚀 30 Days of Java Interview Questions – Day 12 💡 Question: What is the final keyword in Java? 🔹 What is final? final is a keyword in Java used to restrict modification. It can be applied to: • Variables • Methods • Classes 🔹 final Variable Once a variable is assigned, its value cannot be changed. Example: ```java id="k3h9ds" final int x = 10; // x = 20; ❌ Error ``` 🔹 final Method A final method cannot be overridden in a subclass. ```java id="p8f2la" class A { final void show() { System.out.println("Hello"); } } ``` 🔹 final Class A final class cannot be extended (no inheritance allowed). Example: ```java id="z1d8qp" final class A {} // class B extends A ❌ Not allowed ``` ⚡ Quick Summary • final variable → value cannot change • final method → cannot override • final class → cannot inherit 📌 Interview Tip final is widely used to create immutable objects and to improve security and performance in Java applications. Follow this series for 30 Days of Java Interview Questions. #java #javadeveloper #codinginterview #backenddeveloper #softwareengineer #programming #developers #tech
To view or add a comment, sign in
-
-
🚀 30 Days of Java Interview Questions – Day 10 💡 Question: What is Deadlock in Java and how does it occur? 🔹 What is Deadlock? Deadlock is a situation where two or more threads are blocked forever, each waiting for a resource held by the other. --- 🔹 How Deadlock Happens Step 1 Thread 1 locks Resource A Step 2 Thread 2 locks Resource B Step 3 Thread 1 waits for Resource B Thread 2 waits for Resource A Both threads keep waiting forever Deadlock occurs --- 🔹 Example ```java class DeadlockExample { static final Object lock1 = new Object(); static final Object lock2 = new Object(); public static void main(String[] args) { Thread t1 = new Thread(() -> { synchronized(lock1) { System.out.println("Thread 1 locked Resource A"); synchronized(lock2) { System.out.println("Thread 1 locked Resource B"); } } }); Thread t2 = new Thread(() -> { synchronized(lock2) { System.out.println("Thread 2 locked Resource B"); synchronized(lock1) { System.out.println("Thread 2 locked Resource A"); } } }); t1.start(); t2.start(); } } ``` --- 🔹 Key Conditions for Deadlock • Mutual Exclusion • Hold and Wait • No Preemption • Circular Wait --- ⚡ Quick Facts • Deadlock causes program to hang • Common in multithreaded applications • Hard to debug in real systems --- 📌 Interview Tip To avoid deadlock: • Always acquire locks in the same order • Use tryLock() instead of synchronized • Avoid nested locks when possible --- Follow this series for 30 Days of Java. #java #javadeveloper #codinginterview #backenddeveloper #softwareengineer #programming #developers #tech
To view or add a comment, sign in
-
-
JAVA INTERVIEW SCENARIO: The interviewer says: “Let’s see how you think about real Java behavior, not just code.” 1) Your HashMap suddenly starts behaving incorrectly in production. Why? Answer: • equals() and hashCode() not implemented properly • Mutable keys being modified after insertion • High hash collisions degrading performance • Using non-thread-safe HashMap in concurrent environment --- 2) Your Java application shows high GC activity and performance drops. Why? Answer: • Excessive object creation increasing GC pressure • Short-lived objects flooding the heap • Improper memory allocation patterns • Large objects frequently created and discarded --- 3) Your application sometimes processes the same request twice. Why? Answer: • Retry logic without idempotency • Duplicate message processing in async systems • Network timeouts causing re-execution • Missing request deduplication logic --- This is what real Java interviews look like. They don’t test syntax. They test how you reason about real-world problems. Which one would you struggle to explain? Comment below. I’ll share the detailed Java and Spring boot Questions and Answers PDF with interested folks.
To view or add a comment, sign in
-
🚀 30 Days of Java Interview Questions – Day 27 💡 Question: What is the difference between fail-fast and fail-safe iterators in Java? This is a very important and commonly asked interview question in collections. --- 🔹 Fail-Fast Iterator Fail-fast iterators immediately throw an exception if the collection is modified during iteration. They work on the original collection. Example: ```java id="p3k9q1" List<Integer> list = new ArrayList<>(); list.add(1); list.add(2); for (Integer i : list) { list.add(3); // causes exception } ``` Output: ConcurrentModificationException --- 🔹 Fail-Safe Iterator Fail-safe iterators do not throw an exception if the collection is modified. They work on a copy of the collection. Example: ```java id="v7l2m4" CopyOnWriteArrayList<Integer> list = new CopyOnWriteArrayList<>(); list.add(1); list.add(2); for (Integer i : list) { list.add(3); // no exception } ``` --- 🔹 Key Differences Fail-Fast • Throws ConcurrentModificationException • Works on original collection • Faster Fail-Safe • No exception • Works on copy • Slower --- ⚡ Quick Facts • Most Java collections use fail-fast iterators • Fail-safe is used in concurrent collections • Helps avoid unexpected behavior --- 📌 Interview Tip Fail-fast is used for safety and debugging, while fail-safe is used for concurrency. --- Follow this series for 30 Days of Java Interview Questions. #java #javadeveloper #codinginterview #backenddeveloper #softwareengineer #programming #developers #tech
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development