Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Userland autoboxing?

Is it possible to implement autoboxing for your own classes?

To illustrate my example, this is what I might want to write:

Foo foo = "lolcat";

And this is what Java would do (as per my own definitions, somewhere, somehow), under the hood:

Foo foo = new Foo();
foo.setLolcat("lolcat");

So, is this possible somehow, or is it a JVM-feature only?

like image 888
Henrik Paul Avatar asked Nov 04 '08 08:11

Henrik Paul


1 Answers

No, java does not support operator overloading (http://en.wikipedia.org/wiki/Operator_overloading).

Autoboxing is a compiler feature and not available for your own classes.

The reasoning is explained here: http://www.cafeaulait.org/javafaq.html#xtocid1902938

like image 166
jiriki Avatar answered Nov 18 '22 18:11

jiriki