Why doesn't Java offer operator overloading?

前端 未结 17 2367
梦谈多话
梦谈多话 2020-11-22 07:38

Coming from C++ to Java, the obvious unanswered question is why didn\'t Java include operator overloading?

Isn\'t Complex a, b, c; a = b + c; much simpl

17条回答
  •  失恋的感觉
    2020-11-22 08:28

    Alternatives to Native Support of Java Operator Overloading

    Since Java doesn't have operator overloading, here are some alternatives you can look into:

    1. Use another language. Both Groovy and Scala have operator overloading, and are based on Java.
    2. Use java-oo, a plugin that enables operator overloading in Java. Note that it is NOT platform independent. Also, it has many issues, and is not compatible with the latest releases of Java (i.e. Java 10). (Original StackOverflow Source)
    3. Use JNI, Java Native Interface, or alternatives. This allows you to write C or C++ (maybe others?) methods for use in Java. Of course this is also NOT platform independent.

    If anyone is aware of others, please comment, and I will add it to this list.

提交回复
热议问题