codyfranklin1994
codyfranklin1994 Aug 15, 2026 • 20 views

Steps to compare strings in Java with .equals()

Hey everyone! 👋 I'm a student learning Java, and I'm trying to figure out the best way to compare strings. I keep seeing `.equals()` mentioned, but I'm not 100% sure how it works and when to use it. Can someone explain it in a simple way with examples? Thanks! 🙏
💻 Computer Science & Technology
🪄

🚀 Can't Find Your Exact Topic?

Let our AI Worksheet Generator create custom study notes, online quizzes, and printable PDFs in seconds. 100% Free!

✨ Generate Custom Content

1 Answers

✅ Best Answer
User Avatar
johnlewis1989 Dec 31, 2025

📚 Comparing Strings in Java with .equals()

In Java, comparing strings might seem straightforward, but it requires a specific approach to ensure accurate results. The `.equals()` method is the standard way to compare the content of String objects. Let's break down why and how to use it.

When you create a string in Java, you're creating an object. If you use the `==` operator, you are comparing whether two string variables point to the same object in memory. If you want to know if they have the same characters in the same order, use `.equals()`.

✨ Definition of .equals()

The `.equals()` method in Java is a method of the String class that compares the content of two String objects. It returns `true` if the characters in both strings are exactly the same, and `false` otherwise. Case matters!

🔑 Definition of ==

The `==` operator in Java compares the references of two objects. It checks if two variables point to the same object in memory. For String objects, this means checking if they are the exact same instance, not whether they have the same characters.

📝 .equals() vs. == Comparison Table

Feature.equals()==
PurposeCompares the content of stringsCompares the references of string objects
Return Value`true` if the content is identical, `false` otherwise`true` if the variables point to the same object in memory, `false` otherwise
Usage with StringsThe correct way to compare string contentIncorrect way to compare string content in most cases
Example`string1.equals(string2)``string1 == string2`

💡 Key Takeaways

  • ✔️ Always use `.equals()` to compare the content of strings in Java.
  • 🧠 The `==` operator checks if two String variables refer to the same object instance.
  • 📚 Case matters! `"hello".equals("Hello")` returns `false`.
  • 🔗 To ignore case, use `.equalsIgnoreCase()`. For example: `"hello".equalsIgnoreCase("Hello")` returns `true`.
  • 💻 Consider this code:
    String str1 = "example";
    String str2 = "example";
    String str3 = new String("example");
    
    System.out.println(str1.equals(str2)); // Output: true
    System.out.println(str1 == str2);      // Output: true
    System.out.println(str1.equals(str3)); // Output: true
    System.out.println(str1 == str3);      // Output: false

Join the discussion

Please log in to post your answer.

Log In

Earn 2 Points for answering. If your answer is selected as the best, you'll get +20 Points! 🚀