Can Java autoboxing be disabled?

In the Generalize (Upgrade) tutorial at:

http://docs.oracle.com/javase/tutorial/java/generics/types.html

defines a simple Box class:

public class Box {
    private Object object;

    public void set(Object object) { this.object = object; }
    public Object get() { return object; }
}

      

and states:

Since its methods accept or return an object, you can pass whatever you want, as long as it is not one of the primitive types.

Every primitive passed to the set method works without compilation error. Is there a way to prevent autoboxing that automatically wraps the primitive if I really want to break it? And more generally: is there a way to prevent automatic alerts? I am using Java 7.

+3


source to share


2 answers


Is there a way to manually prevent autoboxing?

The only sure way is to use the Java version of Java Java 5 when autoboxing was introduced. This would be a very bad idea.



(Or maybe compiling with the -source flag, which indicates compatibility with Java 1.4 sources. But you will also lose a lot of other modern Java language features: generics, enums, etc.)

Autoboxing / unboxing is a fundamental part of the modern Java language, and cannot be disabled and performed at will.

+4


source


No no. The original type, on which a reference type is expected, will be automatically inserted into the field (assuming the types match).



+4


source







All Articles