Convert Integer to Int in Java

  1. Convert Integer to Int in Java
  2. Integer to Int Conversion in Java 1.4 or Lower
  3. Safe Conversion of Integer to Int in Java
  4. Integer to Int Conversion Using parseInt() Method in Java

This tutorial introduces how to convert Integer to int with some examples in Java.

In Java, Integer is a wrapper class that is used to create integer objects, whereas int is a primitive type that holds a primitive integer value. There can be a scenario when we need to convert the Integer object to primitive int type and vice versa. To convert the Integer to int, we can use the intValue() or the parseInt() method. However, after Java 1.5 version, the Java compiler does this implicitly, and we don’t need any explicit conversion. Before Java 1.5, no implicit conversion was available.

Let’s understand with some examples.

Convert Integer to Int in Java

In this example, we have an Integer object and convert it to a primitive int type. See, we did not use any method or explicit casting, but a simple assignment and the conversion takes place. This is the simplest solution to get a primitive int value from an Integer object. See the example below.

public class SimpleTesting{  
	public static void main(String[] args){
		Integer a = new Integer(10);
		System.out.println("Integer value = "+a);
		int b = a; // implicit conversion
		System.out.println("int value = "+b);
	}
}  

Output:

Integer value = 10
int value = 10

Integer to Int Conversion in Java 1.4 or Lower

If you use the Java 1.4 version or a lower one, then use the intValue() method of the Integer class to convert the Integer object to int type because no implicit conversion is supported. This method does not get any argument, but it will return a primitive value. See the example below.

public class SimpleTesting{
	public static void main(String[] args){
		Integer a = new Integer(10);
		System.out.println("Integer value = "+a);
		int b = a.intValue();
		System.out.println("int value = "+b);
	}
}  

Output:

Integer value = 10
int value = 10

Safe Conversion of Integer to Int in Java

Since an Integer is an object, then it can be null also. So, to avoid any runtime error or exception, use this code example. Here, we used ternary operators to check if the object is null or not and assign any default int value. See the example below.

public class SimpleTesting{  
	public static void main(String[] args){
		Integer a = null;
		System.out.println("Integer value = "+a);
		int b = (a!=null) ? a.intValue() : 0;
		System.out.println("int value = "+b);
	}
}

Output:

Integer value = null
int value = 0

Integer to Int Conversion Using parseInt() Method in Java

The parseInt() is a method of Integer that can convert an integer value to int. It gets a string argument and returns an int value. It is useful if we have string integer object only. See the example below.

public class SimpleTesting{  
	public static void main(String[] args){
		Integer a = new Integer("10");
		System.out.println("Integer value = "+a);
		int b = Integer.parseInt(a.toString());
		System.out.println("int value = "+b);
	}
}  

Output:

Integer value = 10
int value = 10
Write for us
DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Java Integer

  • Reverse an Integer in Java
  • Convert Int to Binary in Java
  • The Max Value of an Integer in Java
  • Minimum and Maximum Value of Integer in Java
  • Related Article - Java Int

  • Reverse an Integer in Java
  • Convert Int to Binary in Java
  • The Max Value of an Integer in Java
  • Minimum and Maximum Value of Integer in Java