Why doesn't Java offer operator overloading?

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

    Assuming Java as the implementation language then a, b, and c would all be references to type Complex with initial values of null. Also assuming that Complex is immutable as the mentioned BigInteger and similar immutable BigDecimal, I'd I think you mean the following, as you're assigning the reference to the Complex returned from adding b and c, and not comparing this reference to a.

    Isn't :

    Complex a, b, c; a = b + c;
    

    much simpler than:

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

提交回复
热议问题