Java 1.4: Cast primitive type to Object (Coupling vs Performance?)
Asked Answered
I

2

6

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?

Interosculate answered 16/3, 2011 at 14:7 Comment(0)
R
9

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.

Remy answered 16/3, 2011 at 14:11 Comment(9)
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.Washboard
I don't think new Integer(int) is slow... just less memory efficient since it can't use the -128 to 127 cache.Mowry
All in all, instantiate an Integer object is better than tighten coupling then?Interosculate
@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.Heavenward
@Konstantin Komissarchik Thanks. Your answer will do.Interosculate
@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. tech.puredanger.com/2007/02/01/valueofRemy
@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.Mowry
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.Mowry
Integer.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.Skidmore
U
5

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;
Unpaged answered 16/3, 2011 at 14:10 Comment(3)
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.Interosculate
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.Unpaged
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.Interosculate

© 2022 - 2024 — McMap. All rights reserved.