(Answered)int vs Integer | SoloLearn: Learn to code for FREE!

+4

(Answered)int vs Integer

int[] x = new int[7]; //works int[] y = new Integer[7] // not works WHAT?? I tried reading on google, but found the language a bit tough, if anyone can explain in easy kiddish language, thanks !!

5/10/2017 10:45:06 AM

Meharban Singh

8 Answers

New Answer

+6

@Meharban, its for you- int is a primitive type. Variables of type int store the actual binary value for the integer you want to represent. int.parseInt("1") doesn't make sense because int is not a class and therefore doesn't have any methods. Integer is a class, no different from any other in the Java language. Variables of type Integer store references to Integer objects, just as with any other reference (object) type. Integer.parseInt("1") is a call to the static method parseInt from class Integer (note that this method actually returns an int and not an Integer). To be more specific, Integer is a class with a single field of type int. This class is used where you need an int to be treated like any other object, such as in generic types or situations where you need nullability. Note that every primitive type in Java has an equivalent wrapper class: byte has Byte short has Short int has Integer long has Long boolean has Boolean char has Character float has Float double has Double Source: Stack Overflow

+6

int = primitive type Integer = Object with int

+5

+4

In java, int is a data type, while Integer is a wrapper class. Since Integer is a class, we used to create it's object, while primitive datatype like int can have variables

+4

Thanks @Sachin , its almost clear, please tell me about wrapper class

+4

0

... Are you being serious? You can't set int to be an Integer.

0