Why doesn't Java offer operator overloading?

前端 未结 17 2156
梦谈多话
梦谈多话 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:27

    Sometimes it would be nice to have operator overloading, friend classes and multiple inheritance.

    However I still think it was a good decision. If Java would have had operator overloading then we could never be sure of operator meanings without looking through source code. At present that's not necessary. And I think your example of using methods instead of operator overloading is also quite readable. If you want to make things more clear you could always add a comment above hairy statements.

    // a = b + c
    Complex a, b, c; a = b.add(c);
    

提交回复
热议问题