Why is int changed to Integer automatically in java?

前端 未结 6 1552
半阙折子戏
半阙折子戏 2021-01-16 15:12

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

相关标签:
6条回答
  • 2021-01-16 15:15

    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

    0 讨论(0)
  • 2021-01-16 15:20

    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

    0 讨论(0)
  • 2021-01-16 15:22

    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.

    0 讨论(0)
  • 2021-01-16 15:30

    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.

    0 讨论(0)
  • 2021-01-16 15:33

    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.

    0 讨论(0)
  • 2021-01-16 15:42

    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.

    0 讨论(0)
提交回复
热议问题