
how to use equals method in String Comparison?

package Compare;
public class StringTest {
public static void main(String [] args){
String str="Rose";
String str1="ROSE";
if (str.equals(str1))
{
System.out.println("The two strings are same.");
}
else{
System.out.println("The two strings are not same.");
}
}
}
Output:
The two strings are not same.
Description:-Here is an example of comparing two strings using equals() method. In the above example, we have declared two string and using the method equals(), we have compared the strings. This method is case sensitive.

and if u want to compare two strings not evn caring whats the case of both strings being compared....then there is method in java called str.equalsIgnoreCase(str1) which will return a boolean having value true here in above example..
e.g.-->
public class StringTest { public static void main(String [] args) { boolean b=false; String str="Rose"; String str1="ROSE"; b= str.equalsIgnoreCase(str1)
System.out.println("The two strings are same-->"+b);
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.