More fundamental reason Java doesn't include operator overloading (at least for assignment)?

Referring to a 2-year discussion that Java does not have operator overloading ( Why Java does not offer operator overloading? ) And based on many intensive years of C ++ in Java, I wonder if there is a more fundamental reason that operator overloading is not part of the Java language, at least in the case of an assignment, than the highest response rating in this communication state near the bottom of the answer (namely, that it was James Gosling's personal choice).

In particular, consider the purpose.

// C++
#include <iostream>

class MyClass
{
public:
    int x;
    MyClass(const int _x) : x(_x) {}
    MyClass & operator=(const MyClass & rhs) {x=rhs.x; return *this;}
};

int main()
{
    MyClass myObj1(1), myObj2(2);
    MyClass & myRef = myObj1;
    myRef = myObj2;

    std::cout << "myObj1.x = " << myObj1.x << std::endl;
    std::cout << "myObj2.x = " << myObj2.x << std::endl;

    return 0;
}

Conclusion:

myObj1.x = 2
myObj2.x = 2

Java, , myRef = myObj2 (, myRef myClass myRef = myObj1, Java, "" Java) - - myObj1.x,

myObj1.x = 1
myObj2.x = 2

++ Java , Java, , " " , Java, (.. myClass myRef = myObj1 myRef Java). , , Java , , , , .

In other words, it is not just a “choice”, and even the inability to “hold your breath” with the hope that it will ever be presented, as the aforementioned high rating also says (near the end). Quote: “The reasons for not adding them now may be a combination of internal politics, an allergy to this function, distrust of developers (you know, saboteurs), compatibility with previous JVMs, time to write the correct specification, etc. Therefore, do not hold your breath, waiting this function. " <- Therefore, this is not true, at least for the assignment operator: the reason why operator overloading (at least for assignment) is not fundamental to the nature of Java.

Is this the correct assessment on my part?

ADDITION

, , : - , , - , ? , "" Java ++ -as-values ​​/reference. .. ++ ( , , - , , ), Java .

+3
4

?

" ". #, , . . -?

- , , ? , "" Java ++ --/.

. - clone() , String. ++, , . , .

, - RAII, , . .

+1

, Java ++, . ++ Java . Java - , ++ . ++, Java , . , , ++ Java, :

int main() {
   type a(1), b(2);
   type *pa = &a, *pb = &b;
   pa = pb;
   // a is still 1, b is still 2, pa == pb == &b
}

: , ++. , , , . , : , .

operator= Java, , :

Type a = new Type(1);
Type b = new Type(2);
a = b;                 // dispatched to Type.operator=( Type )??
a.foo();
a = new Type(3);       // do you want to copy Type(3) into a, or work with a new object?

, , : , ( , ), pass-by-reference, , ( , void foo( type* ) void foo( type& ): - , , .

, , a , a, - ( #), , , , , /, . , , , .

, , , , , , a+b type* operator+( type*, type* ) . , , , a+b - , ( ). , +, =, ==, !=...

, #, . , # , , , . # , , , ( SO, , , X # , X - , .

+5

, , + -. , Java, , , , , , , :

, , , . , , ++. - , , , : , 20-30 , ; - - , , + , , . , , , , - . 10 , , ; , , , , "+", "a + b", a b - . , , , - , . , ? . - , 60 , . , , - , , , , , . , , , - , - . .

UPDATE: Re: , +=, -= .. . swap, void swap(int *a, int *b);. .

+3

, ++, , . , :

  • -
  • - /
  • ++
  • / / - ++
  • .
  • api ++
  • / , / .
  • , ​​ < (ostream &, ostream (* fptr) (ostream &));
  • ++, 2d-.
  • - , .

- , . , , , .

: # 4: :

class A { friend void f(); }; class B { friend void f(); }
void f() { /* use both A and B members inside this function */ }

:

class A { static void f(); }; void f() /* use only class A here */ }

:

class A { }; void f() { /* you have no special access to any classes */ }

# 2: # 10, , , : stdlib:

  ostream &operator<<(ostream &o, std::string s) { ... } // inside stdlib
  int main() std::cout << "Hello World" << std::endl; }

, std:: cout std:: ofstream std:: stringstream. , < ostream. .

№3: . , . 2- , 2d, , , , . №4 , , 2d- . № 8 , 2d. № 7 , .

-1

All Articles