The String class includes many methods that compares Strings or Substrings within strings.
equals() and equalsIgnoreCase()
package stringdemo;
public class StringComparision {
public static void main(String[] args) {
String str="john";
String str1="john";
String str2="JOHN";
String str3=new String("john");
if(str==str1){
System.out.println("Equal");
}else{
System.out.println("Not Equal");
}
if(str.equals(str1)){
System.out.println("str is equal to str1");
}else{
System.out.println("str is not equal to str1");
}
if(str==str2){
System.out.println("str is equal to str2");
}else{
System.out.println("str is not equal to str2");
}
if(str.equals(str2)){
System.out.println("equals");
}else{
System.out.println(" not equals");
}
if(str.equalsIgnoreCase(str2)){
System.out.println("equalsss");
}else{
System.out.println(" not equalsss");
}
if(str1==str3){
System.out.println("str1 is equal to str3");
}else{
System.out.println("str1 is not equal to str3");
}
if(str.equals(str3)){
System.out.println("bang");
}else{
System.out.println("not bang");
}
}
}
The output of the above program is as below:
Equal
str is equal to str1
str is not equal to str2
not equals
equalsss
str1 is not equal to str3
bang
0 comments:
Post a Comment