Java integer type constant declaration – primitive or object


In Java, what's the benefit of int constant declared by an object way:

public final static Integer SOME_CONSTANT = Integer.valueOf(99);

instead of classic

public final static int SOME_CONSTANT = 99;

I know the basic difference between objects and primitives, also autoboxing. But I have seen this declaration in our company's code and I wonder, is there any particular reason of declaring integer constant as an object?

Best Solution

It depends on how you plan to use the constant. If you have an API which requires an Integer, then using the int form will have a small performance penalty because of auto-boxing.

If you just need the primitive, the unboxing of Integer isn't very expensive but easy to avoid.

Also note that

Integer SOME_CONSTANT = Integer.valueOf(99);

is exactly the same as

Integer SOME_CONSTANT = 99;

The *.valueOf() methods were added the API to give the compiler a common way to auto-box primitives.