Here we will learn What Is The Difference Between int And Integer In Java? Where int is a primitive data type and Integer is a wrapper class that gives more flexibility in terms of converting from one form to another.
Primitive Data Types
They are the pre defined data types java that is already defined in java for example int, chat, float, long, etc. The name of primitive data types will always start with a lower case where as it is not the case with a wrapper class.
Wrapper Class
if we talk about a wrapper class it is a class that encapsulates types of data, which could result in being used for creating object instances and methods for another class.
Need Of Wrapper Class
As we know at times we need to convert a type of data into another type Like sometimes we need to convert a string into chars or integers, And it was not possible with primitive data types so here rises the need for a wrapper class.
Where we convert the data type into a class and we can typecast the objects from one form to another in this way we simply convert one type of data to another data type let’s understand it with an example.
public class Main { public static void main(String args[]) { Integer a = new Integer("123"); // Casting not possible // int a = (int)"123"; // Casting not possible // int c="123"; // Casting possible using methods // from Integer Wrapper class int b = Integer.parseInt("123"); System.out.print(a + new Float("10.1")); } }
As explained how an integer is taken as a String and a float and added ad float and returned as result after adding both it is how two different types of data are taken and perform the required actions and again converted to another form and printed.
Conversion Of Primitive Data Types To Wrapper Class
There are wrapper classes corresponding to each primitive data type so here is a list of all the primitive data types in the wrapper class.
Primitive Wrapper Class
- int Interger
- char Charecter
- byte Byte
- short Short
- long Integer
- float Float
- double Double
- boolean Boolean
Learn More about What Is The Difference Between int And Integer In Java?: Int vs Integers
And also Visit JAVA TUTORIALto learn more about java and solution of other problems related to java