.equals()

BrandonDusch580 total contributions
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
Looking to contribute?
- 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.