Search code examples
javacastingprimitive

Custom casting to primitive datatype


I've created my own MutableInt class, which contains a primitive int and some methods for changing the value of this int or just increasing or decreasing it by 1.

I was wondering is there is any way to make this class work like the Integer class, so for example do things like:

MutableInt foo = 42;

or

MutableInt foo = new MutableInt(41);
foo++;

or

MutableInt foo = new MutableInt(42);
int bar = foo;

Is this possible in Java?


Solution

  • No, there's no language support for user-defined conversions in Java. Only the conversions explicitly listed in the specification are supported.