19

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.

8
  • 1
    new Integer( string1 ) == new Integer(string 2) Commented Apr 2, 2013 at 12:35
  • 4
    @madhairsilence. Note that your method will fail, as you are comparing two Integer objects using ==, which might be considered a Crime. Commented Apr 2, 2013 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); Commented Apr 2, 2013 at 12:41
  • 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. Commented Apr 2, 2013 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. Commented Apr 2, 2013 at 12:53

7 Answers 7

24
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.

Sign up to request clarification or add additional context in comments.

1 Comment

Wouldn't this overflow if the numbers are long than what Int can hold
17

Don't forget the BigInteger for very long values.

return new BigInteger(s1).compareTo(new BigInteger(s2));

4 Comments

BigInteger does not accept string.
My apology, I was looking into .NET instead of Java 😹
@Muflix No problem. Have a nice day.
9
Integer.valueOf(string1).equals(Integer.valueOf(string2));

1 Comment

valueOf(String) internally calls parseInt(String) and wraps it. Comparing the results of parseInt(String) using == works without boxing.
4

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));

1 Comment

I also found this answer's explanation very helpful. 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.
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)

Comments

1

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.

Comments

-1

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

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.