Table of Contents
String’s equals method compares String with specified object. It checks if both have same character sequence. It is generally used to compare two Strings.
When you run above program, you will get below output:
Method Signature:
1 2 3 |
public boolean equals(Object anObject) |
String equals Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package org.arpit.java2blog; public class StringEqualsExamplesMain { public static void main(String[] args) { String str1 = "Hello world from java2blog.com"; String str2 = new String("Hello world from java2blog.com"); String str3 = "Hello world from JAVA2BLOG.COM"; if (str1.equals(str2)) { System.out.println("1st and 2nd Strings are equal"); } else { System.out.println("1st and 2nd Strings are not equal"); } if (str1.equals(str3)) { System.out.println("1st and 3rd Strings are equal"); } else { System.out.println("1st and 3rd Strings are not equal"); } } } |
1 2 3 4 |
1st and 2nd Strings are equal 1st and 3rd Strings are not equal |
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.