. Advertisement .
..3..
. Advertisement .
..4..
In Java, Integer and Int are both used for storing the integer data type. However, there is a big difference between them that some IT beginners tend to confuse.
An integer is a class of wrappers, while an int obeys the principles according to a primitive value. In some cases, you will need to know how to convert integer to int in Java. This blog will help you become an expert on it.
Before starting, you must clarify the difference between integer and int. In Java programming language, Integer is seen as a class of wrappers that are used for creating integer objects.
On the other hand, int refers to a primitive type that contains primitive integer values. There are some cases when you need to convert an integer to an int or vice versa.
intValue()
and parselnt()
are the most commonly used methods for converting Java integers to int. When Java 1.5 was released, this conversion was completely conducted by Java compilers. There is no available implicit conversion before that. This blog will introduce some related coding examples as below.
Convert Integer to Int in Java
1. Implicit conversion
The following demonstration will show you how an Integer is converted into a primitive int value.
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
The coding above only applied the simplest technique for conducting the conversion between Integer objects and int values.
2. Use intValue() in Java ver 1.4
You can utilize the method of intValue()
to get an Int from an Integer in Java 1.4 and below. intValue()
is the available method contained in java.lang package.
It is used for returning the integer value as an int. Also, this method will not support implicit conversion. There is no argument about using intValue()
, but the final output will be the primitive value.
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
A Careful Way to Convert an Integer to an Int
Due to the fact that int is a primitive type of data, it becomes storage for any data in binary by default. Hence, int can not present the null value.
On the opposite, an Integer is known as an object, so it definitely can be a null in some cases. To prevent runtime faults or arising exceptions, the below guide will use ternary operators to access whether there is a null object or not, and any int value is assigned in default.
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
4. Use parseInt() to Convert an Integer to an Int in Java
The parselnt()
function is mostly used for the acceptance of the string, radix parameter, and the conversion it to an integer.
However, you can completely utilize it for getting an Int from an integer easily. This parselnt()
method will receive string arguments and then return int values. It will be beneficial in case you only have a string object as an integer.
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
Conclusion
The above tutorials have given you various methods to convert Integer to Int in Java. Moreover, we also help you to differentiate the integer from the int. If you want to learn more about integers, you can find out how to compare integers in Java here!
Leave a comment