This is actually related to a question I asked earlier, but I was left hanging on this detail. I'm restricted to Java 1.4 and I want to cast an int
type to Object
. Do I really need to use an Integer
class object or there's a way to cast it directly (there's no auto-boxing in 1.4). Is the cost of this "manual boxing" worthwhile over importing a whole class from the 3rd layer to the 1st layer, thus increasing coupling?

- 292,901
- 67
- 465
- 588

- 2,310
- 6
- 25
- 52
2 Answers
There is no simple way to convert a primitive to its Object-based twin in Java 1.4 but there is a slow and a fast way. new Integer(int)
is slow, Integer.valueOf(int)
is fast. The same is true for all the other number types.
In Java 5, you don't need as much code but internally, the compiler will insert a call to valueOf()
for you when you use autoboxing.

- 321,842
- 108
- 597
- 820
-
3*X is slow* and *Y is fast* is a bit too categoric, I would say. In most cases (i.e. for values outside the cached range) they do the same (and valueOf has a method calling overhead), and if not, even `new Integer(int)` should not be that slow - only the final variable memory barrier may hit you, if using multiple threads. – Paŭlo Ebermann Mar 16 '11 at 14:15
-
I don't think `new Integer(int)` is slow... just less memory efficient since it can't use the -128 to 127 cache. – ColinD Mar 16 '11 at 14:16
-
All in all, instantiate an Integer object is better than tighten coupling then? – makoshichi Mar 16 '11 at 14:17
-
@S.O.: The only thing that I would add is Java 5 integer autoboxing is just syntactic sugar for Integer.valueOf(int). Do just that in 1.4 code and don't overthink it. – Konstantin Komissarchik Mar 16 '11 at 14:18
-
@Konstantin Komissarchik Thanks. Your answer will do. – makoshichi Mar 16 '11 at 14:20
-
@Paulo, ColinD: Try it. valueOf() is about two times faster. Plus it can save you a huge amount of memory, if you need lots of (small) ints. http://tech.puredanger.com/2007/02/01/valueof/ – Aaron Digulla Mar 16 '11 at 14:23
-
1@Aaron: In the general case (where the argument is not in the range -128 to 127) `valueOf` can _at best_ be slightly slower than `new`, because it has the method call overhead, one or two boolean checks and _then_ a `new`. I don't disagree at all that it's better to use `valueOf`... I just don't think the reason you give for using it is entirely accurate. – ColinD Mar 16 '11 at 14:27
-
For the record, Alex's microbenchmark that you're citing compares `new Integer(0)` to `Integer.valueOf(0)`, showing that `valueOf` is faster for values in the cached range. It isn't a general performance comparison. – ColinD Mar 16 '11 at 14:52
-
2Integer.valueOf(int) did not exist in Java 1.4, only valueOf(String), which doesn't seem to guarantee caching, so new Integer(int) is the only option. – ILMTitan Mar 16 '11 at 15:18
In your Java 1.4 environment, you cannot cast an int to an Object, because it is not an Object.
Java distinguishes between primitive types and reference types. An int is a primitive type. So are boolean, byte, char, short, long, float and double.
A value of reference type is a reference to some object. "Object" is the root class of all objects.
In Java 1.5 and afterward, autoboxing will lead the second variable to point to an Integer object holding the same value as the primitive variable i
.
int i = 99;
Object o = (Object) i;

- 84,978
- 11
- 107
- 151
-
I figured that much. But is using Integer better than increasing coupling? I might pass an Object[] (which would turn out to contanin an Integer and a String) or the whole class, but I'd have to import a class from the 3rd layer to the 1st layer. Which one is better? I'm inclined towards decoupling, as I think the performance hit wouldn't be to great, but I'm no Java expert. – makoshichi Mar 16 '11 at 14:13
-
Coupling may affect architectural flexibility. But coupling to classes in java.lang should be of no concern, and I fail to see how it could affect performance. – Andy Thomas Mar 16 '11 at 14:21
-
Like I said, I'm no Java expert and performance is a really big deal while working with embedded applications. But I'm settled, I'm gonna use an Integer object after all. – makoshichi Mar 16 '11 at 14:25