Convert Int to Integer in Java

Mohammad Irfan Jan 30, 2023 Oct 09, 2021
  1. Convert Int to Integer Using Autoboxing in Java
  2. Convert Int to Integer Using Integer Constructor in Java
  3. Convert Int to Integer Using the Integer.valueOf() Method in Java
Convert Int to Integer in Java

This tutorial introduces how to convert primitive int to an integer object in Java.

Java uses either primitive int type or Integer wrapper class to hold integer values. If we want to convert primitive int to an Integer object, Java provides several methods such as valueOf() and Integer() constructors.

In this article, we will learn to use these methods. So, let’s get started.

Convert Int to Integer Using Autoboxing in Java

Autoboxing is a technique in which a primitive type gets converted into an object implicitly. Its inversion is called unboxing. Java supports autoboxing implicitly, so we don’t need to write any extra code.

In the example below, we used autoboxing to convert int to an Integer object and see both variables hold the same value. See the code example below.

public class SimpleTesting{
	public static void main(String[] args){
		int a = 10;
		System.out.println("a = "+a);
		Integer i = a; // autoboxing
		System.out.println("i = "+i);
	}
}

Output:

a = 10
i = 10

Convert Int to Integer Using Integer Constructor in Java

The Java Integer class is a wrapper class used to create objects of primitive int type. We can use its constructor to convert an int to an Integer object. In the below example, we used the Integer class constructor that takes an int value as an argument and returns it as an Integer object.

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

Output:

a = 10
i = 10

Convert Int to Integer Using the Integer.valueOf() Method in Java

This is another that we can use to convert an int to an Integer in Java. Here, we used valueOf() method of the Integer class. It is a static method that takes an int primitive argument and returns an Integer object. So, we can use this method Here. See the code example below.

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

Output:

a = 10
i = 10

We have seen how to convert an int to an Integer in Java, but here we are giving one more trick to you to verify whether the conversion is successful. It means you can verify the result by using the getClass() method of the Object class. This method returns a fully qualified name of the class(including package name).

We used the getClass() method to check whether the resulted value is an object of the Integer class or not and see it works fine.

We used the getSimpleName() method to get only the name of the class from the fully qualified name. See the example below.

public class SimpleTesting{
	public static void main(String[] args){
		int a = 10;
		System.out.println("a = "+a);
		Integer i = Integer.valueOf(a);
		System.out.println("i = "+i);
		System.out.println(i.getClass().getSimpleName());
	}
}

Output:

a = 10
i = 10
Integer

If we don’t use the getSimpleName() method, the output will be below.

a = 10
i = 10
class java.lang.Integer

Related Article - Java Integer