Userland autoboxing?
Asked Answered
M

1

5

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?

Moonstone answered 4/11, 2008 at 8:17 Comment(0)
C
12

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

Cowshed answered 4/11, 2008 at 8:21 Comment(1)
That's not quite correct. Java doesn't support user-generated operator overloading, it supports operator overloading in its classes when it feels like it (STRINGS!)Distal

© 2022 - 2024 — McMap. All rights reserved.