Why is int changed to Integer automatically in java?
Asked Answered
R

6

1

Why is primitive type int changed to object Integer automatically when i put primitve type int to ArrayList in java?

Ruse answered 4/12, 2012 at 11:25 Comment(2)
autoboxing occurs as collections can only hold objects not primitivesPreceding
You can use TIntArrayList as an alternative if you want to use primitives.Shiri
H
12

Autoboxing automatically converts primitive types to their appropriate wrapper object. The reason behind it is that you can't put a primitive into a collection. Before Java 5 came along you had to do this yourself but now this is handled automatically for you.

See this link here for more details: http://docs.oracle.com/javase/1.5.0/docs/guide/language/autoboxing.html

Heterocyclic answered 4/12, 2012 at 11:26 Comment(0)
G
2

It is called auto-boxing in java.

As any Java programmer knows, 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.

Link

Gasolier answered 4/12, 2012 at 11:27 Comment(0)
S
1

ArrayList can only store objects. int is a primitive data type so it is "auto-boxed" to the object equivalent. This only happens as of Java 5, before that you had to box the Integer yourself.

Stephine answered 4/12, 2012 at 11:28 Comment(0)
P
1

autoboxing occurs as collections can only hold objects not primitives. If you need an int primitive out you will have to unbox it with intValue() method when you read it out.

Preceding answered 4/12, 2012 at 11:28 Comment(0)
T
0

This is a java language feature that was introduced with java 1.5. It's called autoboxing.

Roughly spoken, it converts between java primitive types to their corresponding wrapper class types. The compiler detects when inboxing (primitive-to-wrapper) or outboxing (wrapper-to-primitive) is needed (and possible) and expands the expression the correct byte code.

So, behind the scenes, an instance of Integer is added to the list when you add an int.

Tier answered 4/12, 2012 at 11:32 Comment(0)
P
0

Please read more on reference types and value types to understand this better.

An ArrayList holds references to objects only. It does not hold the value itself.

Since an int is a value type, it does not have a reference. When you convert an int to an Integer, you create some space in the memory for an Integer which holds an int value, and a reference to that Integer object you created.

Now your ArrayList simply holds the address to the Integer object you have instead of keeping the integer itself.

Think it like this: One element in your ArrayList occupies memory block: 200. Your Integer object is in memory block: 400. In memory block 200, instead of keeping the value of the integer, you keep the memory address which is 400.

Reason, I do not know. I guess they just decided to do it this way to keep it simple.

Promptbook answered 4/12, 2012 at 11:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.