4

In my Java class, the professor uses something like:

integerBox.add(new Integer(10));

Is this the same as just doing:

integerBox.add(10);

? I've googled a bit but can't find out one way or the other, and the prof was vague. The closest explanation I can find is this:

An int is a number; an Integer is a pointer that can reference an object that contains a number.

Jeremy
  • 43
  • 1
  • 3
  • What's `integerBox`? Also, with Java's [autoboxing](http://download.oracle.com/javase/1.5.0/docs/guide/language/autoboxing.html), you can transparently mix regular primitive `int` and the `Integer` class instances. – wkl Sep 04 '11 at 00:42
  • In this case, I'm following the tutorial on generics here: http://download.oracle.com/javase/tutorial/java/generics/generics.html I saw this line and remembered a similar things in class. – Jeremy Sep 04 '11 at 00:44
  • *"... and the prof was vague"*. Or alternatively, you did not understand what he told you. – Stephen C Sep 04 '11 at 01:08

3 Answers3

7

Basically, Java collection classes like Vector, ArrayList, HashMap, etc. don't take primitive types, like int.

In the olden days (pre-Java 5), you could not do this:

List myList = new ArrayList();
myList.add(10);

You would have to do this:

List myList = new ArrayList();
myList.add(new Integer(10));

This is because 10 is just an int by itself. Integer is a class, that wraps the int primitive, and making a new Integer() means you're really making an object of type Integer. Before autoboxing came around, you could not mix Integer and int like you do here.

So the takeaway is:

integerBox.add(10) and integerBox.add(new Integer(10)) will result in an Integer being added to integerBox, but that's only because integerBox.add(10) transparently creates the Integer for you. Both ways may not necessarily create the Integer the same way, as one is explicitly being created with new Integer, whereas autoboxing will use Integer.valueOf(). I am going by the assumption the tutorial makes that integerBox is some type of collection (which takes objects, and not primitives).

But in this light:

int myInt = 10;
Integer myInteger = new Integer(10);

One is a primitive, the other is an object of type Integer.

wkl
  • 77,184
  • 16
  • 165
  • 176
  • 1
    Downvote because the paragraph under the "so the takeaway is" section is wrong. integerBox.add(10) is not equivalent to integerBox.add(new Integer(10)) - one creates an integer all the time, one does not always (and usually doesn't) create an integer. – MetroidFan2002 Sep 04 '11 at 05:44
5
integerBox.add(10);

is equivalent to

integerBox.add(Integer.valueOf(10));

So it may return the cached Integer instance.

Read Java Specialist 191 for various way of setting autoboxing cache size.

See also: cache options

Community
  • 1
  • 1
Prince John Wesley
  • 62,492
  • 12
  • 87
  • 94
  • That works for this specific program. Is there an actual difference between the two though? If I understand it correctly, using .add(Integer(10)) creates an Integer object that holds an int of value 10. If I were to just write .add(10), I would only be creating an int of value 10, but not an object. – Jeremy Sep 04 '11 at 00:57
  • Exactly. And for this reason, using Integer.valueOf(foo) is always recommended above 'new Integer(foo)'. It surprises me this is in the generics tutorial like this. – Arnout Engelen Sep 04 '11 at 00:57
  • @Jeremy: it depends on whether 'add' expects an 'int' or an 'Integer'. If it expects an Integer, and you pass it an int of value 10, Java will 'autobox' the int by doing 'Integer.valueof(10)' automatically for you, to convert the int to an Integer. – Arnout Engelen Sep 04 '11 at 00:59
  • @Arnout, so the end result will be the same? If that's so, would it still be a better practice to write Integer.valueof(Foo) vs. just Foo? – Jeremy Sep 04 '11 at 01:03
  • 1
    @Jeremy - use `foo`, autoboxing will take care of it for you. Only real reason to use the explicit `Integer.valueOf(foo)` is if you are using an old Java. – wkl Sep 04 '11 at 01:06
  • @Jeremy, Its usually best practice to write the code as clear and as simple as you can, this often turns out to to perform well too. – Peter Lawrey Sep 04 '11 at 07:11
0

In this case, yes. I'm assuming that integerBox is a collection of objects - you can only store objects within integerBox. This means that you cannot have a primitive value, such as an int, within the collection.

After Java 5 was released, however, there came about something called autoboxing. Autoboxing is the process of automatically converting a primitive value to an object. This is done through one of the wrapper classes - Integer, Double, Character, etc (all named with a capital letter and a name pertaining to the primitive value that they represent).

When you added int 10 to the collection(ArrayList, most likely), the Java VIrtual Machine transformed it into an object of type Integer behind the scenes.

vladdy
  • 9
  • 3