String compares function: equals() And compareTo()

In this blog, we will see equals V/S compareTo , equals V/S equalsIgnoreCase, comapreTo V/S compareToIgnoreCase and How to use string compares function in programs.

Let’s see example and video on String compares Function

 

import java.util.*;

class str3

{

                public static void main(String args[])

                {

                                // equals V/S equlasIgnoreCase  – Both are same when both string is totally different

                                String s1 = “Hello”;

                                String s2 = “Bye”;

                                System.out.println(s1.equals(s2)); // Return boolean value -> true | false  :-> false

                                // both string same then return true otherwise return false

                                System.out.println(s1.equalsIgnoreCase(s2));   

// Return boolean value -> true | false  :-> false

 

                                // Now check when both are same but case different :-> upperCase or LowerCase

                                String s3 = “Hello”; // e is lower case

                                String s4 = “HEllo”; // E is upper case

                                // Let’s check equals and equalsIgnoreCase

                                System.out.println(“different between equals and equalsIgnoreCase”);

                                System.out.println(s3.equals(s4));           // return false

                                System.out.println(s3.equalsIgnoreCase(s4)); // return true

 

                                // Now we will see different between equals and compareTo

                                // equals() – return boolean value true | false :-> both string same -> true otherwise false

                                // compareTo() – retrun int value

// 0 – both equals

// >0 – string1 > string2

// <0 – string1 < string2

                                String s5 = “Hello”;

                                String s6 = “HeLlo”;

                                System.out.println(“different between equals and compareTo”);

                                System.out.println(s5.equals(s6)); // return false

                                System.out.println(s5.compareTo(s6)); // return int value

                                // Let’s check compareTo function

                                // First -> Check H ASCII Code – H ASCII Code =0

                                // sencond -> Check e ASCII Code – e ASCII Code = 0

                                // third -> Check l ASCII Code – L ASCII Code = 32

                                // String is not equlas

 

                                // different between comparTo and compareToIgnoreCase

                                // Same as equals and equlasIgnoreCase -> Try your self

 

                                // How to use in Program

                                Scanner in1 = new Scanner(System.in);

 

                                int x = 56;

                                int y = 78;

                                System.out.print(“Enter Value Of operator : “);

                                String op = in1.next();

                                int ans = 0;

                                if(op.compareTo(“+”) == 0)         // op check with “+” operator

                                {

                                                ans = x+y;

                                }

                                else if(op.compareTo(“>”) == 0)

                                {

                                                ans = (x>y) ? x : y;

                                }

                                System.out.println(“Answer = ” + ans);

 

                }

}

Output:

False

False

Different between equals and equalsIgnoreCase

False

True

Different between equals and compareTo

False

32

Enter Value Of Operator : +

Answer = 134


Video available on YouTube.


[sbscrbr_form]

If you have any query on blog example, you can directly comment us or subscribe our website


Earn Money Online

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

468total visits,3visits today


mayank

Hello, I am Maddy a Java blogger, trainer and developer

Related articles

1 Comment

  1. November 2, 2016 - 5:46 pm

    I benefit from browsing your web site. thnx!

Leave a Reply

Your email address will not be published. Required fields are marked *

Your comments (*)

Name (*)

Your full name please.

Email address (*)

Used for gravatar.

Website

Link back if you want.

Enter Captcha Here :*

Reload Image

EmailEmail
PrintPrint