mathematical operands used on objects


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: mathematical operands used on objects

  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.

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


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center