Java Program to Compare Two Strings (JNNC Technologies)


This Java program is used to demonstrates comparison of two strings.
  • Java equals() method is used to compare strings.
  • Java equalsIgnoreCase() method can ignore the case.
  • We cannot use == operator to compare two strings.
Example:
public class EqualCheck {
    public static void main(String args[]){
        String a = "AVATAR";
        String b = "avatar";

        if(a.equals(b)){
            System.out.println("Both strings are equal.");
        } else {
            System.out.println("Both strings are not equal.");
        }

        if(a.equalsIgnoreCase(b)){
            System.out.println("Both strings are equal.");
        } else {
            System.out.println("Both strings are not equal.");
        }
    }
}
Program Output:
JavaEqualCheck

Post a Comment

0 Comments

'; (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();