Java String equals()

Last Updated On Wednesday 29th Jun 2022

equal java

The equals() method returns true if two strings are equal. If not, it returns false.

Syntax

	string.equals(String str)
	

Parameters

  • str – the string to be compared

Returns

  • returns true if the strings are equal
  • returns false if the strings are not equal
  • returns false if the str argument is null
	class Main {
  public static void main(String[] args) {
     String myStr1 = "Code";
     String myStr2 = "Jagd";
     String myStr3 = "Something Another str";
     System.out.println(myStr1.equals(myStr2));
     System.out.println(myStr1.equals(myStr3));
  }
}
	

Output

	false
false
	

java equals

	class Main {
  public static void main(String[] args) {
    String str1 = "Python";
    String str2 = "JavaScript";
    String str3 = "Python";
    boolean result;

    result = str1.equals(str2);
    System.out.println(result);

    result = str1.equals(str3);
    System.out.println(result);

    result = str3.equals(str1);
    System.out.println(result);
  }
}
	

With Above code , returns

  • str1 and str2 are not equal. Hence, str1.equals(str2) returns false.
  • str1 and str3 are equal. Hence, str1.equals(str3) returns true
  • str3 and str1 are equal. str3.equals(str1) returns true.

Output

	false
true
true
	
equals java
java string equals
equal in java
equals to java
equals java
java equals
java string equals
javascript equals
compare strings java
java compare strings
equals