How To Compare String And Integer In Java

How To Compare String And Integer In Java | We will discuss how to compare string and integer in Java. The integer is a wrapper class for an int primitive and the String class is given to store characters in Java.

In order to compare two different types, we have to convert one of the types to another. For example, here we have to convert string to integer or integer to string and then perform the comparison. To achieve the given task we have two ways:-

  1. Convert integer to string type and then compare.
  2. Convert the string to an integer type and then compare.

The second way is not appropriate because only those strings can be converted to the Integer/int which is holding the number. If it is holding strings like “apple”, and “hello” then we can’t convert those strings to the Integer type. Though it can be done, but we will discuss only the first way i.e. convert integer to string type and then compare

How To Compare String And Integer In Java

We will take an Integer value and convert it into a string. After that we can use the equals() method or compareTo() method of the String class to compare the value.

public class Main {
    public static void main(String[] args) {
        Integer num1 = 10;
        String string1 = "apple";
        String string2 = "10";

        // convert Integer value to String
        String str1 = Integer.toString(num1);

        System.out.println(str1.equals(string1));
        System.out.println(str1.equals(string2));
    }
}

Output:-

false
true

The equals() method return true or false but if we want to get the difference between characters then we can take the help of the compareTo() method.

public class Main {
    public static void main(String[] args) {
        Integer num1 = 10;
        String string1 = "apple";
        String string2 = "10";

        // convert Integer value to String
        String str1 = Integer.toString(num1);

        System.out.println(str1.compareTo(string1));
        System.out.println(str1.compareTo(string2));
    }
}

Output:

-48
0

How To Compare String And Int In Java

Comparing String and int primitive is very similar to the comparison of String and Integer object values. Let us see it through an example.

public class Main {
    public static void main(String[] args) {
        int num1 = 10;
        String string1 = "2";
        String string2 = "10";

        // convert int value to string
        String str = String.valueOf(num1);

        System.out.println(str.equals(string1));
        System.out.println(str.equals(string2));
    }
}

Output:-

false
true

public class Main {
    public static void main(String[] args) {
        int num1 = 10;
        String string1 = "2";
        String string2 = "10";

        // convert int value to string
        String str = String.valueOf(num1);

        System.out.println(str.compareTo(string1));
        System.out.println(str.compareTo(string2));
    }
}

Output:-

-1
0

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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