Advertisement



< Prev
Next >



String equals() method




The equals() method in String class performs an equality check between the values present in two String objects.




Signature of equals()


public boolean equals(String st)






Note :


This method performs case-sensitive equality check.




Let's understand equals() method by an example -:


Here in the next example, we have initialized two String objects and we are checking for the equality in their values by calling equals() method on one of them.
// Program to find whether value of String is equal as value of another String.


class StringEquals
{
public static void main(String[] ar)
{
String str1= new String("A sunny day");
String str2= new String("A sunny day");

System.out.println("First  String is : " + str1);
System.out.println("Second String is : " + str2);

System.out.println("Are values in first and second String object similar?  " + str1.equals(str2));

String str3= new String("Hello321");
String str4= new String("Hello32");

System.out.println("Third  String is : " + str3);
System.out.println("Fourth String is : " + str4);

System.out.println("Are values in third and fourth String object similar?  " + str3.equals(str4));
}
}


Output is :


First  String is : A sunny day
Second String is : A sunny day
Are values in first and second String object similar?  true
Third  String is : Hello321
Fourth String is : Hello32
Are values in third and fourth String object similar?  false


Program Analysis





Advertisement




Method equals() performs case-sensitive equality check


// Program performing a case-sensitive equality check between two String values.


class StringEquals
{
public static void main(String[] ar)
{
String str1= new String("United States");
String str2= new String("United states");

System.out.println("First String is "  + str1);
System.out.println("Second String is " + str2);

System.out.println("Are values in first and second String object similar?  " + str1.equals(str2));


String str3= new String("India");
String str4= new String("india");

System.out.println("Third String is :"  + str3);
System.out.println("Fourth String is :" + str4);

System.out.println("Are values in third and fourth String object similar?  " + str3.equals(str4));


}
}


Output is :


First  String is United States
Second String is United states
Are values in first and second String object similar?  false
Third  String is :India
Fourth String is :india
Are values in third and fourth String object similar?  false


Program Analysis






Please share this article -




< Prev
Next >
< replace() method
String Comparison >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement