java tutorial - Java Strings equals() method - java programming - learn java - java basics - java for beginners



 Java Strings equals() method

Learn java - java tutorial - Java Strings equals() method - java examples - java programs

Description

Java - The equals () method - compares the string with the specified object. The result is true only if the argument is not null and is a string object (String) that represents the same sequence of characters as this object.

Syntax

The syntax of the method is:

public boolean equals(Object anObject)
click below button to copy the code. By - java tutorial - team

Options

Detailed information about the parameters:

  • anObject is the object with which to compare this string.

Return value

  • In Java, equals () returns true if the lines are equal; otherwise false.

Sample Code

public class Test {

   public static void main(String args[]) {
      String Str1 = "Welcome to wikitechy.com";
      String Str2 = Str1;
      String Str3 = new String("A site for learning programming");
      boolean retVal;

      retVal = Str1.equals(Str2);
      System.out.println("Is Str1 equal to Str2? Answer: " + retVal );

      retVal = Str1.equals(Str3);
      System.out.println("Is Str1 equal to Str3? Answer: " + retVal );
   }
}
click below button to copy the code. By - java tutorial - team

Output :

Is Str1 equal to Str2? Answer: true
Is Str1 equal to Str3? Answer: false

Related Searches to Java Strings equals() method