Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
HowToDoInJava
  • Java
  • Spring AI
  • Spring Boot
  • Hibernate
  • JUnit 5
  • Interview

Java String.equals()

Learn to compare the content of two String objects in a case-sensitive manner using the String.equals() API. For case-insensitive comparison, we can use the equalsIgnoreCase() method. Never use ‘==’ operator for checking the strings equality. It verifies the object references, not the content, which is undesirable in most cases. 1. …

Lokesh Gupta

January 9, 2023

Java String class
Java Compare, Java String
Java String

Learn to compare the content of two String objects in a case-sensitive manner using the String.equals() API. For case-insensitive comparison, we can use the equalsIgnoreCase() method.

Never use '==' operator for checking the strings equality. It verifies the object references, not the content, which is undesirable in most cases.

1. String.equals() API

The String.equals() in Java compares a string with the object passed as the method argument. It returns true if and only if:

  • the argument object is of type String
  • the argument object is not null
  • represents the same sequence of characters as the current string
String str1 = "alex";
String str2 = "alex";
String str3 = "alexa";

Assertions.assertTrue(str1.equals(str2));
Assertions.assertFalse(str1.equals(str3));

2. Throws NullPointerException

The equals() does not support null argument and throws NullPointerException.

String str1 = "alex";

Assertions.assertThrows(NullPointerException.class, () -> {
  str1.contains(null);
});

3. Only Case-sensitive Comparisons

The following Java program demonstrates that equals() method does the content comparison in a case-sensitive manner. If we change the case, strings are considered different.

String str1 = "alex";

Assertions.assertTrue(str1.equals("alex"));
Assertions.assertFalse(str1.equals("Alex"));    //different case is used

4. Difference between Equals Operator and equals() Method

As mentioned earlier, '==' operator checks for the same object references. It does not check for string content. Whereas equals() method strictly checks for string content only.

In the following Java program, we have created two String objects. First we are comparing the objects using the equals operator which results in false because both are different objects in memory.

Then we check the content of strings using the equals() that returns true because even though objects are different, their content is same.

String strObj1 = new String("test");
String strObj2 = new String("test");

Assertions.assertFalse(strObj1 == strObj2);
Assertions.assertTrue(strObj2.equals(strObj2));

Happy Learning !!

Reference: Java String Doc

Sourcecode on Github

Comments

Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

String Examples

  • String Constant Pool
  • Convert String to int
  • Convert int to String
  • Convert String to long
  • Convert long to String
  • Convert CSV String to List
  • Java StackTrace to String
  • Convert float to String
  • Align Left, Right, Center
  • Immutable Strings
  • StringJoiner
  • Split a string
  • Escape HTML
  • Unescape HTML
  • Convert to title case
  • Find duplicate words
  • Left pad a string
  • Right pad a string
  • Reverse recursively
  • Leading whitespaces
  • Remove whitespaces
  • Reverse words
  • Find duplicate characters
  • Get first 4 characters
  • Get last 4 characters
  • (123) 456-6789 Pattern
  • Interview Questions

String Methods

  • String concat()
  • String hashCode()
  • String contains()
  • String compareTo()
  • String compareToIgnoreCase()
  • String equals()
  • String equalsIgnoreCase()
  • String charAt()
  • String indexOf()
  • String lastIndexOf()
  • String intern()
  • String split()
  • String replace()
  • String replaceFirst()
  • String replaceAll()
  • String substring()
  • String startsWith()
  • String endsWith()
  • String toUpperCase()
  • String toLowerCase()

Table of Contents

  • 1. String.equals() API
  • 2. Throws NullPointerException
  • 3. Only Case-sensitive Comparisons
  • 4. Difference between Equals Operator and equals() Method
Photo of author

Lokesh Gupta

A fun-loving family man, passionate about computers and problem-solving, with over 15 years of experience in Java and related technologies. An avid Sci-Fi movie enthusiast and a fan of Christopher Nolan and Quentin Tarantino.
Follow on Twitter Portfolio

Previous

Java String charAt()

Next

Java String equalsIgnoreCase()

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Tutorial Series

OOP

Regex

Maven

Logging

TypeScript

Python

Meta Links

About Us

Advertise

Contact Us

Privacy Policy

Our Blogs

REST API Tutorial

Follow On:

  • Github
  • LinkedIn
  • Twitter
  • Facebook
Copyright © 2026 | Sitemap