(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 !!

10th May 2017, 10:45 AM
Meharban Singh
Meharban Singh - avatar
8 Antworten
+ 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
10th May 2017, 10:53 AM
Sachin Artani
Sachin Artani - avatar
+ 6
int = primitive type Integer = Object with int
10th May 2017, 1:15 PM
NimWing Yuan
NimWing Yuan - avatar
+ 5
­čśâ
10th May 2017, 10:57 AM
Sachin Artani
Sachin Artani - avatar
+ 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
10th May 2017, 10:51 AM
Sachin Artani
Sachin Artani - avatar
+ 4
Thanks @Sachin , its almost clear, please tell me about wrapper class
10th May 2017, 10:52 AM
Meharban Singh
Meharban Singh - avatar
+ 4
Thanks
10th May 2017, 10:57 AM
Meharban Singh
Meharban Singh - avatar
0
... Are you being serious? You can't set int to be an Integer.
10th May 2017, 10:49 AM
Eldar Bakerman
0
^
10th May 2017, 3:48 PM
Eldar Bakerman