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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

operator overloading

Status
Not open for further replies.

ajuking001

Programmer
Mar 30, 2001
3
JP
Hi all,
We can overload a method in Java. But can we do so to an operator ? What is operator overloading ? It is possible in C++.(i don't know c++) Could anyone explain what it is how its done with an example.

Archie.
 
Sorry, operator overloading is not possible in Java. In C++ you can define any operators (like +, -, * and so on) newly for any class you want. In Java this is not possible, you have to create methods like
Code:
myObject.add(myOtherObject)
to achieve this and
Code:
myObject + myOtherObject
will result in an error at compile time.

The only operator that is a little overloaded is +: If you want to concatenate Strings you can use it like in the following short example:

Code:
System.out.println("Hello " + "World");

I've not programmed in C++ anymore for a pretty long time now, so I can't tell you how it's done there - hop over to the C++ forum for that. allow thyself to be the spark that lights the fire
 
Hi

"Operator overloading" allows you to overload the language's primitives operators, such as + , - , new , etc.
For example you could overload the + operator to make it handles the addition of nxn matrices.
It brings great facilities to client code, and it is quite meaningful.

But in Java there is no operator overloading, perhaps because this concept may be hard to use.

Bye. --
Globos
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top