Java 1.4: Casting a primitive type to an object (communication and performance?)

This is actually related to the question I asked earlier, but I remained hanging on this detail. I am limited to Java 1.4 and I want to use a type intfor Object. I really need to use the class object Integeror is there a way to use it directly (no automatic boxing in 1.4). Is the cost of this โ€œmanual boxโ€ facing the import of an entire class from the third layer to the 1st level, thereby increasing the connection?

+3
source share
2 answers

There is no easy way to convert a primitive to its Object double based in Java 1.4, but there is a slow and fast way. new Integer(int)slow, Integer.valueOf(int)fast. The same is true for all other room types.

In Java 5, you do not need so much code, but inside, the compiler will insert a call valueOf()for you when you use autoboxing.

+7
source

In your Java 1.4 environment, you cannot use int for an object because it is not an object.

Java distinguishes between primitive types and reference types. Int is a primitive type. Booleans, bytes, char, short, long, floating and double.

The value of the reference type is a reference to some object. An โ€œobjectโ€ is the root class of all objects.

Java 1.5 Integer , i.

    int i = 99;
    Object o = (Object) i;
+2

All Articles