Java String to int, Java int to String

Filed Under: Java

Today we will look at Java String to int conversion and then java int to String conversion. Java provides several ways to convert String to int and int to String.

Java String to int

Java String to int, Java int to String

  • Using Integer.parseInt method
  • Using Integer.valueOf method
  • Using above methods with different radix.

Java String to int example

Let’s write a simple java program to convert String to an int, note that String is an Object whereas int is a primitive data type. An integer can be written in Decimal, Hexadecimal, Octal Binary or any other nonstandard formats.

Java 7 one of the feature is Binary Literals. In this example, we will explore how to convert String to int using different radix values.


package com.journaldev.string;

public class JavaStringToInt {

	public static void main(String[] args) {
		String str = "10";
		
		int x = Integer.parseInt(str);
		
		System.out.println("Java String to int value using Integer.parseInt() = "+x);
		
		
		int y = Integer.valueOf(str);
		System.out.println("Java String to int value using Integer.valueOf() = "+y);
		
		String binary = "0100"; //4
		int a = Integer.valueOf(binary, 2);
		int b = Integer.parseInt(binary, 2);
		System.out.println("Java Binary String to int value = "+a);
		System.out.println("Java Binary String to int value = "+b);
		
		String octal = "0100"; //64
		int c = Integer.valueOf(octal, 8);
		int d = Integer.parseInt(octal, 8);
		System.out.println("Java Octal String to int value = "+c);
		System.out.println("Java Octal String to int value = "+d);
		
		String hexadecimal = "0100"; //256
		int e = Integer.valueOf(hexadecimal, 16);
		int f = Integer.parseInt(hexadecimal, 16);
		System.out.println("Java hexadecimal String to int value = "+e);
		System.out.println("Java hexadecimal String to int value = "+f);
		
		String customRadix = "0100"; //25
		int g = Integer.valueOf(customRadix, 5);
		int h = Integer.parseInt(customRadix, 5);
		System.out.println("Java String with radix 5 to int value = "+g);
		System.out.println("Java String with radix 5 to int value = "+h);
		
	}

}

Output of the above program is:


Java String to int value using Integer.parseInt() = 10
Java String to int value using Integer.valueOf() = 10
Java Binary String to int value = 4
Java Binary String to int value = 4
Java Octal String to int value = 64
Java Octal String to int value = 64
Java hexadecimal String to int value = 256
Java hexadecimal String to int value = 256
Java String with radix 5 to int value = 25
Java String with radix 5 to int value = 25
  • Interegr.parseInt(String str) is the most commonly used method to convert String to int, if String represents int in decimal format. Internally, it invokes method Interegr.parseInt(String str, int radix) with radix value as 10.
  • By passing redix value as 2, we can convert Binary int representation String to int, same is true for other known formats Octal, Hexadecimal also.
  • Minimum radix value is 2 and maximum radix value is 36. For radix 36, it includes 0 to 9 and then A(10) to Z(36). The conversion from String to int is case insensitive.
  • Integer.valueOf(String str) is also a way to convert String to int. There is also an overloaded method where we can pass radix, as you can see from above example code.
  • If the String representation does not contain a parsable integer, both above methods throw NumberFormatException.
  • The String representation should only contain the value and not the radix prefix associated with int, like 0x, 0b etc.

Java int to String

  • Using + operator to concatenate int to string.
  • Using String.valueOf method.
  • Using String.format method

Java int to String example

Let’s write a simple program to convert java int to String object using different methods.


package com.journaldev.string;

public class JavaIntToString {

	public static void main(String[] args) {
		int i = 10;
		int j = 0xEF;
		int k = 0123;
		int l = 0b111;
		String str = "" + i;
		String str1 = String.valueOf(j);
		String str2 = String.format("%d", k);
		String str3 = "" + l;

		System.out.println(i + " decimal int to String " + str);
		System.out.println(j + " hexadecimal int to String " + str1);
		System.out.println(k + " octal int to String " + str2);
		System.out.println(l + " binary int to String " + str3);
	}

}

The output of the above program is:


10 decimal int to String 10
239 hexadecimal int to String 239
83 octal int to String 83
7 binary int to String 7
  • +” operator is overloaded for String and it can be used to convert int to String. It also takes care of different formats like Hexadecimal, Octal, Binary and convert them to decimal format before returning the final String object.
  • String.valueOf(int i) can be used to convert int to String. valueOf() method is overloaded to accept float, long, double etc. Internally it uses Integer.toString(int i) method to return the String object.
  • String.format(String format, Object... args) is also another way to convert int to String object.
  • Before converting int to string, value is always converted to decimal format and then assigned to string. As you can see from the output above, all the string representations are in the decimal format.

That’s all for Java String to int and int to String conversion, we will cover more java interview questions in future posts.

Reference: Integer API Doc

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages