12:52 am - Thursday July 27, 2017

Compare equality of two string value boolean equals(Object anObject)

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

                                    This method compares the string to the specified object. It displays the boolean output either true or false. If both the strings are equal the output is true.The output is false if strings are not equal or else if one string is null. It is case-sensitive.

Example Program


public class Rough {

public static void main(String[] args)

{

String s1 = new String("Hi Srilekha");

String s2 = new String("HI SRILEKHA");

String s3 = new String("Hi Srilekha");

boolean b = s1.equals(s2);

boolean b1 = s2.equals(s3);

boolean b2 = s3.equals(s1);

boolean b3 = s3.equals(null);

System.out.println(b);

System.out.println(b1);

System.out.println(b2);

System.out.println(b3);

}

}

Output

  false

  false

  true

  false

Description

                      In this program three strings s1,s2 and s3 are declared and each have a value. If both the string values are equal (s1&s2, s2&s3, s3&s1) the output is true else false. For the fourth boolean value b3, the string s3 compares with null since it has no value it returns the output as false.

Filed in: Java Examples

Comments are closed.