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.

3
  • What's integerBox? Also, with Java's autoboxing, you can transparently mix regular primitive int and the Integer class instances. Commented Sep 4, 2011 at 0:42
  • In this case, I'm following the tutorial on generics here: download.oracle.com/javase/tutorial/java/generics/generics.html I saw this line and remembered a similar things in class. Commented Sep 4, 2011 at 0:44
  • "... and the prof was vague". Or alternatively, you did not understand what he told you. Commented Sep 4, 2011 at 1:08

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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

6 Comments

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.
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.
@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, 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 - 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.
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.