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.
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL