Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do you overload an operator?

Status
Not open for further replies.

Legionaire

Technical User
May 8, 2003
33
US
n00b question:

I got 1 class. And two instances of it.

There're values in those objects that I want to add together with the following format:

object1 + object2 = certain member elements added together;

it's the same as overloading with the friend operator in C++.
 
Java does not support operator overloading. While it is a great language it does not compare to C++. Syntax is similar but not much beyond that.

-pete
 
"Jave does not support overator overloading" is correct with one exception : The binary + operator is the only example of operator overloading in java.

"primitive numeric type" + "primitive numeric type" = addition
"string" + "any data type" = string concatenation
"any data type" + "string" = string concatenation
(In the last 2 cases an implicit string conversion will take place (via the toString() method

Any other combination gives a compiler error.
 
hologram, nice observation. However the question was about writing operator functions in your own classes which C++ supports.

-pete
 
Interestin!! This is not possible in java except if you use the + operator.To do what you want you need to store the values in variables and then operate on them from 2 different objects.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top