DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2004
    Posts
    635

    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);

  2. #2
    Join Date
    Apr 2005
    Posts
    3
    No, unless you are using some third-party preprocessing tools.

  3. #3
    Join Date
    Mar 2005
    Location
    Sendling, MUC, .de
    Posts
    100
    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


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


Sponsored Links