➤ How to Code a Game
➤ Array Programs in Java
➤ Java Inline Thread Creation
➤ Java Custom Exception
➤ Hibernate vs JDBC
➤ Object Relational Mapping
➤ Check Oracle DB Size
➤ Check Oracle DB Version
➤ Generation of Computers
➤ XML Pros & Cons
➤ Git Analytics & Its Uses
➤ Top Skills for Cloud Professional
➤ How to Hire Best Candidates
➤ Scrum Master Roles & Work
➤ CyberSecurity in Python
➤ Protect from Cyber-Attack
➤ Solve App Development Challenges
➤ Top Chrome Extensions for Twitch Users
➤ Mistakes That Can Ruin Your Test Metric Program
Int to Integer in Java | In this blog, we are converting the primitive data type int to the Integer object. The int is the primitive data type whereas the Integer is a wrapper class. The wrapper class is the way to use the primitive data types in Java as objects. So here we are basically converting the data type to an object. Also see:- Int vs Integer Java, Convert Integer to int in Java
Following are the different ways to convert int to Integer in Java programming language:-
- AutoBoxing
- valueOf(-) Method
- Integer class Constructor
Convert Int To Integer in Java using AutoBoxing
When we assign an int primitive value to an Integer type variable then due to the autoboxing feature, Java internally converts primitive int to an Integer object. Hence to convert int to integer in Java using autoboxing we have to just assign the int value to the Integer object. The below program demonstrates it.
public class Main {
public static void main(String[] args) {
int num = 10;
Integer num1 = num; // autoboxing
System.out.println(num1);
System.out.println(num1.getClass().getName());
System.out.println(num1.doubleValue());
}
}
Output:-
10
java.lang.Integer
10.0
We can also directly assign value to an Integer object without creating an int-type variable. Let us see it through an example:-
public class Main {
public static void main(String[] args) {
Integer num1 = 99; // autoboxing
System.out.println(num1);
System.out.println(num1.getClass().getName());
System.out.println(num1.doubleValue());
}
}
Output:-
99
java.lang.Integer
99.0
Convert Int To Integer Java using valueOf() Method
Integer class contains the valueOf() method to create an Integer object from an int type value. Following is the prototype of the valueOf() method:- public static Integer valueOf(int i)
The valueOf() method is a static method. Integer class belongs to java.lang package which is the default package available to every Java program therefore we don’t need to import the Integer class explicitly.
public class Main {
public static void main(String[] args) {
int num = 50;
Integer num1 = Integer.valueOf(num);
// Integer num1 = Integer.valueOf(50);
System.out.println(num1);
System.out.println(num1.getClass().getName());
System.out.println(num1.doubleValue());
}
}
Output:-
50
java.lang.Integer
50.0
Convert Int To Integer Java using Constructor
We can also take the help of the constructor of Java Integer class to convert int to Integer object. However, the constructors of all wrapper classes including the Integer class are deprecated and can be removed in the upcoming version. Therefore it is not recommended to use the constructors of any wrapper class. Instead, we should use the valueOf() method of those wrapper classes to convert primitive to wrapper class Object.
public class Main {
public static void main(String[] args) {
int num = 50;
// not recommended
Integer num1 = new Integer(num);
// Integer num1 = new Integer(50);
System.out.println(num1);
System.out.println(num1.getClass().getName());
}
}
Output:-
50
java.lang.Integer
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!