About this question
Wondering how to compare strings in Java? This guide explains different ways to compare strings, including equals(), ==, and compareTo(), and when to use each method.
Wondering how to compare strings in Java? This guide explains different ways to compare strings, including equals(), ==, and compareTo(), and when to use each method.
Log in to share your answer and help other learners.
Log in to answerBest Answer · By JanBask Java Expert
Answered on Mar 27, 2025
If you’re new to Java, you might be wondering how to compare strings properly. Unlike primitive types like int or double, strings in Java are objects, so you can’t always compare them with ==. Here are some common and effective ways to compare strings in Java:
Using equals() method (Best for content comparison)
The equals() method compares the actual content (characters) of two strings. This is the most reliable way to check if two strings are the same.
String str1 = "Hello";
String str2 = "Hello";
if (str1.equals(str2)) {
System.out.println("Strings are equal!");
}Using == operator (Checks memory reference)
The == operator checks if both strings point to the same memory location. It doesn’t compare the actual content, so it might not always work as expected.
String str1 = new String("Hello");
String str2 = new String("Hello");
if (str1 == str2) {
System.out.println("Strings are equal!"); // This won’t print because `==` checks references.
}Using compareTo() method (Lexicographical comparison)
This method compares strings based on dictionary order. It returns 0 if the strings are equal, a positive number if the first string is greater, and a negative number if it’s smaller.
String str1 = "apple";
String str2 = "banana";
int result = str1.compareTo(str2);
System.out.println(result); // Outputs a negative number because "apple" comes before "banana".In most cases, you should stick with equals() for content comparison. The compareTo() method is useful when sorting strings lexicographically, like in alphabetical order!
Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.
Most-read guides across JanBask
Common Java interview questions, answered
Guides, tips and career advice on Java from JanBask experts.
Java Frequently Asked J2EE Interview Questions and Answers You Must Read
Top 45 frequently asked Java J2EE interview questions to ace your interview. These J2EE questions are useful for freshers and…
Java Top 100+ Angular Interview Questions and Answers
Are you preparing for your next Angular interview? We have compiled the list of the top 100+ Angular Interview Questions &…
Java Top 51+ Simple Java Project Ideas for Beginners In 2025
Enhance your Java skills with our top 51+ project ideas! From beginner-friendly exercises to advanced applications, fuel your…
Java How Long Does It Takes To Learn Java?
Confused about How long does it take to learn Java ? Well, No worries! This blog will guide you with valuable information and…