.equals()
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