-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathInteger and int
More file actions
32 lines (20 loc) · 1.2 KB
/
Integer and int
File metadata and controls
32 lines (20 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Question -> I am confused as to why Integer and int can be used interchangeably in Java even though
one is a primitive type and the other is an object?
For example:
Integer b = 42;
int a = b;
Or
int d = 12;
Integer c = d;
Answer:
You can’t put an int (or other primitive value) into a collection. Collections can only hold object
references, so you have to box primitive values into the appropriate wrapper class (which is "Integer" in
the case of int). When you take the object out of the collection, you get the Integer that you put in;
if you need an int, you must unbox the Integer using the "intValue()" method. All of this boxing and unboxing
is a pain, and clutters up your code. The autoboxing and unboxing feature automates the process,
eliminating the pain and the clutter.
That is basically it in a nutshell. It allows you take advantage of the Collections Framework
for primatives without having to do the work yourself.
The primary disadvantage is that it confuses new programmers, and can lead to messy/confusing
code if it's not understood and used correctly.
This matter is taken from the following link: http://stackoverflow.com/questions/7121581/why-can-integer-and-int-be-used-interchangably