Data Types in Java:

Data types represent the different values to be stored in the variable. In java, there are two types of data types:

  • Primitive data types
  • Non-primitive data types

  • For the other primitive types (char, byte, short, int, long, float, and double), there are two kinds of conversion: implicit and explicit.
  • Implicit conversions: An implicit conversion means that a value of one type is changed to a value of another type without any special directive from the programmer.

Example codes on how to convert Java String To Int.

  • Convert using Integer.parseInt()
  • Convert using Integer.valueOf()
  • Convert using new Integer(String).intValue()
  • Convert using DecimalFormat
  • Convert with special radix (not base 10 system)

Convert using Integer.parseInt()

The Integer.parseInt() static method parses the string argument as a signed decimal integer and returns an int value.

Syntax

public static int parseInt(String s) throws Number Format Exception

The parameter s will be converted to a primitive int value. Note that the method will throw a NumberFormatException if the parameter is not a valid int.

Example

[pastacode lang=”java” manual=”String%20numberAsString%20%3D%20%E2%80%9C5678%22%3B%0A%0Aint%20number%20%3D%20Integer.parseInt(numberAsString)%3B%0A%0ASystem.out.println(%22The%20number%20is%3A%20%22%20%2B%20number)%3B” message=”java code” highlight=”” provider=”manual”/] [ad type=”banner”]

When you run the above code, the output will show this:

The number is: 5678

Note: The resulting value is not an instance of the Integer class but just a plain primitive int value.

Convert using Integer.valueOf()

The Integer.valueOf() static method will return an Integer object holding the value of the specified String.

Syntax

public static Integer valueOf(String s) throws NumberFormatException

Note: The method will throw a NumberFormatException if the parameter is not a valid integer.

Example

[pastacode lang=”java” manual=”String%20number%20As%20String%20%3D%20%E2%80%9C5678%22%3B%0A%0Aint%20number%20%3D%20Integer.valueOf(numberAsString)%3B%0A%0ASystem.out.println(%22The%20number%20is%3A%20%22%20%2B%20number)%3B” message=”java code” highlight=”” provider=”manual”/]

This will output:

The number is: 5678

Note: The resulting value is an instance of the Integer class and not a primitive int value.

Convert using new Integer(String).intValue()

Another alternative method is to create an instance of Integer class and then invoke it’s intValue() method.

Example

[pastacode lang=”java” manual=”String%20number%20As%20String%20%3D%20%225678%22%3B%0A%0AInteger%20intObject%20%3D%20new%20Integer(numberAsString)%3B%0A%0Aint%20number%20%3D%20intObject.intValue()%3B” message=”java code” highlight=”” provider=”manual”/] [ad type=”banner”]

We can shorten to:

[pastacode lang=”java” manual=”String%20number%20As%20String%20%3D%20%225678%22%3B%0A%0Aint%20number%20%3D%20new%20Integer(numberAsString).intValue()%3B” message=”java code” highlight=”” provider=”manual”/]

or just:

[pastacode lang=”java” manual=”int%20number%20%3D%20new%20Integer(%225678%22).intValue()%3B” message=”java code” highlight=”” provider=”manual”/]

Convert using Decimal Format

The class java.text.DecimalFormat is a class that can be used to convert a number to it’s String representation or it can parse a String into it’s numerical representation.

Example

[pastacode lang=”java” manual=”String%20numberAsString%20%3D%20%225678%22%3B%0ADecimalFormat%20decimalFormat%20%3D%20new%20DecimalFormat(%22%23%22)%3B%0Atry%20%0A%7B%0A%20%20%20int%20number%20%3D%20decimalFormat.parse(numberAsString).intValue()%3B%0A%20%20%20System.out.println(%22The%20number%20is%3A%20%22%20%2B%20number)%3B%0A%7D%0A%20catch%20(ParseException%20e)%20%0A%7B%0A%20%20%20System.out.println(numberAsString%20%2B%20%22%20is%20not%20a%20valid%20number.%22)%3B%0A%7D%0A” message=”java code” highlight=”” provider=”manual”/]

Output

The number is: 5678

Note: The parse() method will throw a ParseException when there are problems encountered with the String.

Convert with special radix

The above examples uses the base (radix) 10. There are cases when we wish to convert a Java String to Integer but using another base.

Both Integer.parseInt() and Integer.valueOf() can receive a custom radix to be used in the conversion.

Example

[pastacode lang=”java” manual=”Binary%20%0AString%20numberAsString%20%3D%20%2211111111%22%3B%0Aint%20wikinumber1%20%3D%20Integer.valueOf(numberAsString%2C%202)%3B%0Aint%20wikinumber2%20%3D%20Integer.parseInt(numberAsString%2C%202)%3B%0ASystem.out.println(wikinumber1)%3B%0ASystem.out.println(wikinumber2)%3B%20%0A” message=”java code” highlight=”” provider=”manual”/] [ad type=”banner”]

output:

255

255

Octal

[pastacode lang=”java” manual=”String%20numberAsString%20%3D%20%22377%22%3B%0Aint%20wikinumber1%20%3D%20Integer.valueOf(numberAsString%2C%208)%3B%0Aint%20wikinumber2%20%3D%20Integer.parseInt(numberAsString%2C%208)%3B%0ASystem.out.println(wikinumber1)%3B%0ASystem.out.println(wikinumber2)%3B%20%0A” message=”java code” highlight=”” provider=”manual”/]

output:

255

255

Hexadecimal

[pastacode lang=”java” manual=”String%20numberAsString%20%3D%20%22ff%22%3B%0Aint%20wikinumber1%20%3D%20Integer.valueOf(numberAsString%2C%2016)%3B%0Aint%20wikinumber2%20%3D%20Integer.parseInt(numberAsString%2C%2016)%3B%0ASystem.out.println(wikinumber1)%3B%0ASystem.out.println(wikinumber2)%3B%20%0A” message=”java code” highlight=”” provider=”manual”/]

output:

255

255

Categorized in: