🔹 Java Interview Concept: Difference between == and equals() Many Java beginners get confused between == and equals(). 1️⃣ == Operator • Compares memory reference (address). • Checks whether two references point to the same object. Example: String a = new String("Java"); String b = new String("Java"); System.out.println(a == b); // false 2️⃣ equals() Method • Compares the actual content (values). • Used for logical equality. Example: System.out.println(a.equals(b)); // true 💡 Interview Tip: Always use equals() for content comparison in objects like String, Integer, etc. #Java #CodingInterview #JavaDeveloper #Programming #TechLearning
Java == vs equals() Method
More Relevant Posts
-
💡 A Java Interview Favorite: Immutable Classes While revising Java interview concepts today, I explored how immutable classes work in Java and why they are widely used in real-world applications. Here are a few key learnings: 🔹 What is a POJO? A simple Java object where data is accessed through getters and setters. 🔹 What is an Immutable Object? An object whose state cannot be changed after it is created. Once initialized, its values remain constant. (javaspring) 🔹 Steps to Create an Immutable Class ✔️ Declare the class as final ✔️ Make all fields private and final ✔️ Initialize fields through a constructor ✔️ Do not provide setter methods ✔️ Provide only getter methods (Java Guides) 🔹 Role of the final keyword The final keyword prevents modification— • Final variables cannot be reassigned • Final methods cannot be overridden • Final classes cannot be extended (GeeksforGeeks) 📌 A great real-world example of an immutable class in Java is String. Understanding immutability helps build thread-safe, predictable, and secure applications, which is why it is frequently asked in Java interviews. 🎥 Video I learned from: https://lnkd.in/dqZ-YKWE #Java #JavaDeveloper #ImmutableObjects #JavaInterview #BackendDevelopment #LearningInPublic #Programming
05. How to create immutable class - Java Interview Questions
https://www.youtube.com/
To view or add a comment, sign in
-
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
To view or add a comment, sign in
-
💡 Java Interview Question - String Immutability What will be the output? String s = "hello"; s.concat(" world"); System.out.println(s); Output: hello Why? Strings in Java are immutable. The concat() method creates a new String object instead of modifying the existing one. In this case, the result of concat() is not assigned back, so the original value remains unchanged. Correct approach: s = s.concat(" world"); Takeaway: If you don’t assign the result of a String operation, the change is lost. #Java #InterviewPrep #Programming
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
-
-
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
-
-
🚀 Java Interview Question What is the difference between: ArrayList vs LinkedList? ArrayList ✔ Faster for reading ✔ Uses dynamic array LinkedList ✔ Faster for inserting/deleting ✔ Uses doubly linked list Which one do you use more often? #Java #Programming #CodingInterview
To view or add a comment, sign in
-
🚀 Java Multithreading – Interview Guide (Part 1) I’ve compiled a set of real-world and tricky multithreading questions that helped me deeply understand concepts—not just for interviews, but for writing correct concurrent code. This document covers: ✅ start() vs run() ✅ Thread lifecycle & common traps ✅ Runnable deep dive ✅ volatile vs Atomic classes ✅ Synchronization basics and hidden pitfalls ✅ Real-world bugs (ExecutorService, ThreadLocal, silent failures) 💡 Focus is not just on answers, but why things behave the way they do internally (JVM + memory model) 📌 This is Part 1 — more advanced topics (locks, deadlocks, concurrency utilities, etc.) If you're preparing for: Java interviews Backend roles Or want to write better concurrent code, this would be really helpful. If you notice any issues, improvements, or have questions, feel free to reach out to me — happy to discuss and learn together! Happy learning! 🙌 #Java #Multithreading #InterviewPreparation #BackendDevelopment #Concurrency
To view or add a comment, sign in
-
If you can’t explain these 50+ Java concepts… you’re not ready for placements. Most students “learn Java”… But fail in interviews. Why? Because they focus on syntax, not concept clarity. Top companies don’t ask easy code. They test your fundamentals. Here are 50+ Java concepts you MUST master 👇 👉 If you master these… You can solve 80% of Java interview questions. Don’t just “watch tutorials.” Revise. Practice. Explain. That’s how you stand out. Want the complete list + interview questions PDF? Comment JAVA and I’ll send it. Share this with your friends who are preparing 🚀 Follow Yeshwanth Chintaginjala for more! #java #codinginterview #placements #softwareengineering #developers #programming #careeradvice
To view or add a comment, sign in
-
🚀 30 Days of Java Interview Questions – Day 29 💡 Question: What is the difference between ArrayList and LinkedList in Java? --- 🔹 ArrayList • Based on dynamic array • Fast random access O(1) • Slow insertion/deletion in middle --- 🔹 LinkedList • Based on doubly linked list • Fast insertion/deletion O(1) • Slow random access O(n) --- 🔹 Key Differences Access Time ArrayList → Fast LinkedList → Slow Insertion/Deletion ArrayList → Slow LinkedList → Fast Memory ArrayList → Less LinkedList → More --- 🔹 Example ```java id="a1k9z3" List<Integer> list = new ArrayList<>(); list.add(10); list.add(20); System.out.println(list.get(1)); ``` ```java id="b7m2q8" List<Integer> list = new LinkedList<>(); list.add(10); list.add(20); list.add(0, 5); ``` --- ⚡ Quick Facts • Both implement List interface • Both maintain insertion order • Not synchronized by default --- 📌 Interview Tip Use ArrayList for fast access Use LinkedList for frequent insertions/deletions --- 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
-
-
🚨 Java Interview Question: 👉 What is the difference between Comparable and Comparator? Both Comparable and Comparator are used in Java to sort objects, but they work in different ways. 💡 1️⃣ Comparable Comparable is used when a class itself defines its natural sorting order. ✔ Sorting logic is written inside the class ✔ Uses compareTo() method ✔ Located in java.lang package 💻 Example class Student implements Comparable<Student> { int id; public int compareTo(Student s) { return this.id - s.id; } } Here, Student objects will be sorted by id. 💡 2️⃣ Comparator Comparator is used when we want to define multiple sorting logics outside the class. ✔ Sorting logic written in a separate class ✔ Uses compare() method ✔ Located in java.util package 💻 Example class NameComparator implements Comparator<Student> { public int compare(Student s1, Student s2) { return s1.name.compareTo(s2.name); } } Here, sorting can be done by name instead of id. 🧠 Real-Life Example Imagine a school student list 📚 Students can be sorted by: Roll number Name Marks 🔹 Comparable Default sorting rule (like roll number). 🔹 Comparator Custom sorting rules (like marks or name). 🎯 Strong Interview One-Liner 👉 Comparable defines natural sorting inside the class, whereas Comparator allows custom sorting logic outside the class. #Java #JavaCollections #Comparable #Comparator #JavaDeveloper #InterviewPreparation #Programming
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
Thanks Mam, for sharing this knowledge to us 👏