mathematical operands used on objects
I know you can do this in c++, but what about java?
Is there a way I can define my class so that i could do the following with objects:
objectResult = objectA + objectB;
objectResult = objectA / objectB;
objectResult = objectA * objectB;
Instead of having to do:
objectResult = objectA.add(objectB);
No, unless you are using some third-party preprocessing tools.
Yes, this so-called operator overloading they did not incorporate in the language purposefully.
However, there is one overloaded operator in java, namely +:
Both '1+2' AND ' "x"+"y" ' are legal expressions in java, ie. it's used for addition as well as for string concatenation.
: Beware of bugs in the above code - I have only proved it correct, not tried it.
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center