0

I am confused between these three codes

    int a = 5;
    int b = 6;
    int c = 5;

and

    Integer x = new Integer(5);
    Integer y = new Integer(6);
    Integer z = new Integer(5);

and

    Integer i = 5;
    Integer j = 6;
    Integer k = 5;

I know that the first one are some variables that contain values and the second one are some variables reference to some different objects, but what is the third one? I know that they are reference data types.

But I can't understand why and how many objects created... if any!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
M.Moi
  • 11
  • 1
  • 15
    the third cases uses autoboxing. See http://stackoverflow.com/questions/27647407/why-do-we-use-autoboxing-and-unboxing-in-java – Klas Lindbäck Sep 15 '16 at 13:15
  • in this case this there are 2 different references, in a different case there could be 3. [refering to Integer caching](http://stackoverflow.com/questions/3131136/integers-caching-in-java) – SomeJavaGuy Sep 15 '16 at 13:16
  • 1
    The third case is extra tricky because of the `IntegerCache` built into the `Integer` class. The third case creates no new objects at all - it instead initialises `i` and `j` to the reference to the cached `Integer` object that holds the value `5`, and `j` to the reference to the cached `Integer` object that holds the value `6`. This means that `i == k` returns `true`, whereas `x == z` returns `false`. – JonK Sep 15 '16 at 13:24

2 Answers2

2

Integer is a (wrapper) class name and so the variables of this type are objects. When x is assigned the value of new Integer(5) then, ethically, you cannot use x directly for mathematical operations. To use x in operations, you have to use the wrapper class method intValue() to get x's value in numeric (primitive) form from object form.

Eg.:

Integer x = new Integer(5);
int my_x = x.intValue();
int y = 10 + my_x; //y = 15  

However, using x directly in mathematical ops would not produce an error and, in fact, will produce the same output as going through the above step will. This is because the JVM implicitly converts the object to primitive data value and this process is called Auto boxing. The inverse, when implicitly done, is called Un-boxing. Therefore, in the above example, you can also do this:

Integer x = new Integer(5);
int y = 10 + x; //y = 15;

Hope you now understand.

progyammer
  • 1,498
  • 3
  • 17
  • 29
  • "then, ethically, you cannot use x directly for mathematical operations. " -- Really? Ethically? I wasn't aware that coding in Java required a particular direction on ones moral compass. – scottb Sep 15 '16 at 13:28
  • 1
    I said ethical because that's how the teacher teaches right? You are first instructed to use `intValue()` and then are introduced to autoboxing and unboxing. :-P – progyammer Sep 15 '16 at 13:29
  • I use x for great justice. Take off every Zig. – scottb Sep 15 '16 at 13:32
  • Yes .. that's it .. thank you – M.Moi Sep 15 '16 at 14:05
1

int a = 5; stores a primitive int

Integer x = new Integer(5) stores a primitive int wrapped in an Integer object.

Integer i = 5; Also stores a primitive int wrapped in an Integer object, but the compiler takes care of wrapping it for you. This is called "autoboxing"

You can also do the reverse and write int foo = new Integer(5). This is called "auto unboxing".

Gustav Karlsson
  • 1,151
  • 9
  • 25