Java .equals()
Published Jun 30, 2022Updated Aug 27, 2025
Contribute to Docs
The .equals() method returns true if two strings are equal in value. Otherwise, false is returned.
Syntax
string.equals(object);
The object can either be a string literal or a representation of a String value. This will return true if the string has the same character sequence as object.
Example
The following example showcases the .equals() method:
import java.io.*;public class MyClass {public static void main(String[] args) {// With string literalsSystem.out.println("4".equals("four"));// With string objectsString myFavoriteLanguage = "Java";String codeNinjasFavoriteLanguage = "Java";System.out.println(myFavoriteLanguage.equals(codeNinjasFavoriteLanguage));}}
This will print the following output:
falsetrue
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Java on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
- Beginner Friendly.17 hours