Why can Integer and int be used interchangably?
Asked Answered
J

7

14

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;
Judiejudith answered 19/8, 2011 at 12:31 Comment(0)
J
13

The first few sentences of the posted article describe it pretty well:

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.

Justajustemilieu answered 19/8, 2011 at 12:37 Comment(0)
S
8

Java supports autoboxing and automatically wraps primitive values into objects and unwraps objects to primitive values for certain types, like char - Character, int - Integer, double - Double, etc.

Note from Oracle Documentation:

So when should you use autoboxing and unboxing? Use them only when there is an “impedance mismatch” between reference types and primitives, for example, when you have to put numerical values into a collection. It is not appropriate to use autoboxing and unboxing for scientific computing, or other performance-sensitive numerical code. An Integer is not a substitute for an int; autoboxing and unboxing blur the distinction between primitive types and reference types, but they do not eliminate it.

Stalactite answered 19/8, 2011 at 12:32 Comment(0)
L
4

Because of autoboxing and autounboxing http://download.oracle.com/javase/1.5.0/docs/guide/language/autoboxing.html

Lovering answered 19/8, 2011 at 12:34 Comment(0)
G
4

Using an int and an Integer "interchangeably" is called autoboxing. This feature was introduced in Java 5. Before that, your example code wouldn't have compiled. Instead, you would have to write something like this:

Integer b = Integer.valueOf(42); // or new Integer(42);
int a  = b.intValue();

or

int d = 12;
Integer c = Integer.valueOf(d); // or new Integer(d);

That's fairly verbose, which is why autoboxing was introduced. It's a bit of compiler magic to make life easier for the coder.

Technically, int and Integer themselves are not interchangeable and one cannot be used where the other is required. However, autoboxing allows implicit conversion between the two.

As a side note, there is one case where autoboxing (specifically unboxing) fails. If your code tries to autounbox a null value, you will get a NullPointerException at runtime, e.g.:

Integer b = null;
int a = b; // NullPointerException here!

Just something to be aware of...

Gina answered 23/8, 2011 at 1:4 Comment(0)
R
1

It's called AutoBoxing. That will explain exactly what it is.

Religiosity answered 19/8, 2011 at 12:34 Comment(0)
K
0

In addition to other answers, because Integer is a wrapper class, that lets you box and unbox an int value. Other info here.

Katey answered 19/8, 2011 at 13:32 Comment(0)
B
-1

The java language specification states that the java virtual machine must perform automatic boxing/unboxing for integers and a few other types.

Byblow answered 19/8, 2011 at 12:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.