

#JAVA CONVERT STRING TO INT CODE#
This can be explained quickly: the executed code is the same in all cases. Performance of the various String-to-int conversion methods using Java 8Īs you can see, the first five measurements are almost identical.
#JAVA CONVERT STRING TO INT PLUS#
ParseUnsignedInt() positive value with plus The following table shows the performance of the various method calls using Java 8: Method The test results are in the results/ directory. You can find the source code of this test in my GitHub repository. Performance of various String-to-int conversion methods

But what happens if we use the wrong method? We have seen above that there are separate methods to convert a String to an int primitive or an Integer object. Its hexadecimal representation, converted to the decimal system, corresponds to the input value. parseUnsignedInt() covers the range up to 2 * Integer.MAX_VALUE + 1 – with the result in the range above Integer.MAX_VALUE always being a negative number.parseInt() also covers the range up to Integer.MIN_VALUE and returns exactly the value passed.In the range 0 to Integer.MAX_VALUE, parseInt() and parseUnsignedInt() return the same results.Let's add some debug output to the code above:


If it is 1, the number is negative (for details on negative numbers, see this Wikipedia article). In an int – which is always signed in Java – the first bit stands for the sign. Then why can we assign the number to the int variable hex? The binary representation of the numbers plays an (intended) trick on us. This number is higher than Integer.MAX_INT and therefore, cannot be represented as an int. If you convert "cafebabe" to the decimal system, you get 3,405,691,582. The reason is that the parseInt() method assumes the given number to be positive unless a minus sign precedes it. Why can't this String be converted back to an int? This attempt results in the following error: Exception in thread "main" : For input string: "cafebabe"įirst of all: The String s contains "cafebabe" as expected. Int i = Integer.parseInt(s, 16) Code language: Java ( java ) It becomes interesting (not to say: confusing) if, for example, we convert the valid int value 0xCAFEBABE into a hex String and then back into an int: In all the above cases, the number to be parsed must be within the range Integer.MIN_VALUE (= -2 31 = -2,147,483,648) to Integer.MAX_VALUE (= 2 31-1 = 2,147,483,647). A hexadecimal number can be parsed as follows: The parameter radix specifies the base of the number system. Integer i = Integer.valueOf(s, radix) (→ JavaDoc).int i = Integer.parseInt(s, radix) (→ JavaDoc).To parse other number systems, the following overloaded methods are available: Integer.parseInt("1,000") // Thousands separator not allowed.Integer.parseInt("3.14") // Decimal point not allowed.Integer.parseInt(" 1") // Space not allowed.Integer.parseInt("") // Empty string not allowed.The following Strings are not allowed and result in NumberFormatExceptions: The String to convert must contain only digits, optionally with a plus or minus sign in front. The second method internally calls the first method and converts the result to an Integer object. Integer i = Integer.valueOf(s) (→ JavaDoc).int i = Integer.parseInt(s) (→ JavaDoc).Let's first look at the options to parse a String into an int (or Integer).
