Page 422 - CA_Blue( J )_Class10
P. 422

Difference between == and equals() methods are:

                                   == operator                                           equals()
               Checks whether both the characters are equal or not.  Checks whether both the strings are equal or not.
               Example char ch1=A , ch2=a;                         Example Sting st1= India, st2=INDIA;
               System.out.println(ch1==ch2);                       System.out.println(st1.equals(st2));
               Ans: true                                           Ans: false

              16.3.11 The equalsIgnoreCase() Method
              The equalsIgnoreCase() method is similar to the equals() method and also checks whether the string of the current
              String object whose method is called is same as the string value passed as the parameter. But, the difference is that it
              is not case-sensitive. It returns true if both the strings are equal even if they are of different cases otherwise returns
              false. The syntax to use the equalsIgnoreCase() method is:
                  boolean <variable> = <String_Object>.equalsIgnoreCase(String str);
              For example:
                  String str1= "India";
                  String str2= "INDIA";
                  boolean check= str1.equalsIgnoreCase(str2);
                  if(check)
                      System.out.println("The strings are equal.");
                  else
                      System.out.println("The strings are not equal.");
              You will get the following output:
              The strings are equal.

              16.3.12 The compareTo() Method
              The compareTo() method compares the given String in the parameter with current String object alphabetically. It
              returns integer value, the difference between the ASCII codes of the characters that are compared. If both the strings
              are equal, it returns 0. If the first string is larger lexicographically than the second string, it returns a positive number
              else returns a negative value. Comparison is based on the first string:
                      if str1>str2: it returns a positive value
                      if str1<str2: it returns a negative value
                      if str1==str2: it returns 0
              The syntax of to use the compareTo() method is :

                  int <variable> = Stringobject.compareTo(String str);
              Example 1:

                  String str1= "India";
                  String str2= "INDIA";
                  int value= str1.compareTo(str2);
                  System.out.println("Value: "+ value);
              You will get the following output:
              Value: 32
              Because the difference between the ASCII codes of n and N = 110 - 78 = 32. The comparison is between n and N as the
              first characters of both the strings are equal.
              Example 2:
                  String str1= "INDIAN";
                  String str2= "INDIA";
                  int value= str1.compareTo(str2);
                  System.out.println("Value: "+ value);



                420420  Touchpad Computer Applications-X
   417   418   419   420   421   422   423   424   425   426   427