Why doesn't Java offer operator overloading?

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

    Assuming you wanted to overwrite the previous value of the object referred to by a, then a member function would have to be invoked.

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

    In C++, this expression tells the compiler to create three (3) objects on the stack, perform addition, and copy the resultant value from the temporary object into the existing object a.

    However, in Java, operator= doesn't perform value copy for reference types, and users can only create new reference types, not value types. So for a user-defined type named Complex, assignment means to copy a reference to an existing value.

    Consider instead:

    b.set(1, 0); // initialize to real number '1'
    a = b; 
    b.set(2, 0);
    assert( !a.equals(b) ); // this assertion will fail
    

    In C++, this copies the value, so the comparison will result not-equal. In Java, operator= performs reference copy, so a and b are now referring to the same value. As a result, the comparison will produce 'equal', since the object will compare equal to itself.

    The difference between copies and references only adds to the confusion of operator overloading. As @Sebastian mentioned, Java and C# both have to deal with value and reference equality separately -- operator+ would likely deal with values and objects, but operator= is already implemented to deal with references.

    In C++, you should only be dealing with one kind of comparison at a time, so it can be less confusing. For example, on Complex, operator= and operator== are both working on values -- copying values and comparing values respectively.

提交回复
热议问题