17

I have a simple doubt. Would be great if anyone helps me.

I have two strings:

String string1 = "4"; // and 
String string2 = "04";

Both the values are equal but how to compare them in java? We have equals and equalsIgnoreCase for comparing String alpha values, similarly how to compare numeric values.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
mee
  • 821
  • 5
  • 12
  • 28
  • 1
    new Integer( string1 ) == new Integer(string 2) – madhairsilence Apr 02 '13 at 12:35
  • 3
    @madhairsilence. Note that your method will fail, as you are comparing two `Integer` objects using `==`, which might be considered a Crime. – Rohit Jain Apr 02 '13 at 12:39
  • 3
    @RohitJain please note that integers created by `new` are not 'cached'. `Integer a = 1;` is not the same as `Integer a = new Integer(1);` – msi Apr 02 '13 at 12:41
  • @msi. Oouch. Didn't notice that. – Rohit Jain Apr 02 '13 at 12:42
  • @RohitJain 1. I gave an idea!! Thats why I commented not made an answer 2. What do u mean by Cache?? - Plz Explain 3. What is the crime in comparing with == – madhairsilence Apr 02 '13 at 12:48
  • 1
    @madhairsilence. 2. Java Caches the Integer objects in a certain range, which is generally `[-128, 127]`, in which case, it will not create a new Integer object when you assign an integer literal to an Integer reference, as stated by `msi`. I was mistaken, Cache doesn't hold tru in your case. – Rohit Jain Apr 02 '13 at 12:51
  • 1
    @madhairsilence. 3. Crime with object comparison with `==` is that, it doesn't really compares the content, rather the value of reference. So, two object reference pointing to two objects having same value will not be judged equal by `==`, so you should always use `equals` method for object comparison. – Rohit Jain Apr 02 '13 at 12:53
  • @RohitJain yeh correct! dnt notice dat! – madhairsilence Apr 02 '13 at 13:09

7 Answers7

23
Integer.parseInt("4") == Integer.parseInt("04")

That is it. You can convert a numeric string into integer using Integer.parseInt(String) method, which returns an int type. And then comparison is same as 4 == 4.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
16

Don't forget the BigInteger for very long values.

return new BigInteger(s1).compareTo(new BigInteger(s2));
marstran
  • 26,413
  • 5
  • 61
  • 67
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
10
Integer.valueOf(string1).equals(Integer.valueOf(string2));
Deepak Bala
  • 11,095
  • 2
  • 38
  • 49
  • `valueOf(String)` internally calls `parseInt(String)` and wraps it. Comparing the results of `parseInt(String)` using `==` works without boxing. – Blank Chisui Mar 14 '16 at 16:50
3

Use the Integer class to convert the strings to integers.

http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt%28java.lang.String%29

Integer.parseInt(string1) == Integer.parseInt(string2)
Afshin Moazami
  • 2,092
  • 5
  • 33
  • 55
Colin D
  • 5,641
  • 1
  • 23
  • 35
3

Skipping parsing it to an int at all to handle arbitrarily large numbers, only comparing the strings. As a bonus it also works for decimal numbers (unless there are trailing zeroes, where you probably can do similar thing with anchoring end of string instead).

You can omitt the trim if you are sure there are no whitespace characters

    String string1 = "4"; // and
    String string2 = "04";

    String noLeadingZeroes1 = string1.trim().replaceFirst("^0+(?!$)", "");
    String noLeadingZeroes2 = string2.trim().replaceFirst("^0+(?!$)", "");

    System.out.println(noLeadingZeroes1.equals(noLeadingZeroes2));
Viktor Mellgren
  • 4,318
  • 3
  • 42
  • 75
  • I also found this answer's explanation very helpful. https://stackoverflow.com/a/2800839/3966480 The ^ anchor will make sure that the 0+ being matched is at the beginning of the input. The (?!$) negative lookahead ensures that not the entire string will be matched. – Al-Alamin Sep 20 '18 at 13:02
0

The comparison happens on chacracter basis. so '2' > '12' is true because the comparison will happen as '2' > '1' and in alphabetical way '2' is always greater than '1' as unicode. SO it will comeout true.

yakob abada
  • 1,101
  • 14
  • 20
-1

if(String.valueOf(S1).compareTo(String.valueOf(S2)) < 0)

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 20 '22 at 05:52