.equals()
Published Jun 30, 2022
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 codeNinjasFavoriteLangauge = "Java";System.out.println(myFavoriteLanguage.equals(codeNinjasFavoriteLangauge));}}
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
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn Java
Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.Beginner Friendly16 hours