Computing.Net > Forums > Programming > Java operator overloading

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Java operator overloading

Reply to Message Icon

Name: movitto
Date: April 14, 2004 at 13:02:28 Pacific
OS: Red Hat 8.0
CPU/Ram: P2
Comment:

Hi;

I have alot experience in C++ and now learning Java. When I try to compile a class file I get the following error for every operator I try to overload:

';' expected

number operator +(number temp){

(there is a carrot ^ symbol pointing to +)
Where number is my class I am working with.

Is this because Java does not support operator overloading? If it doesnt is there a way to write the same syntax as above without defining an 'add' function such as

number add(number team)

add calling it with z.add(y)

(where z and y are objects of type number)

Thanks alot



Sponsored Link
Ads by Google

Response Number 1
Name: gpp
Date: April 14, 2004 at 13:16:38 Pacific
Reply:

Operator overloading is not supported, and to my knowledge, theres no way around it.


0

Response Number 2
Name: Infinite Recursion
Date: April 14, 2004 at 13:58:47 Pacific
Reply:

Operator overloading is not supported in Java.

Perhaps you can make use of Groovy...

"Various operators in Groovy are mapped onto regular Java method calls on objects. This allows you the developer to provide your own Java or Groovy objects which can take advantage of operator overloading. The following table describes the operators supported in Groovy and the methods they map to."

http://groovy.codehaus.org/operators.html

I've never used Groovy. So I cannot help you with syntax, etc.

IR


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Java operator overloading

Doubt in Operator OverLoading www.computing.net/answers/programming/doubt-in-operator-overloading/10220.html

Operator overloaded.... www.computing.net/answers/programming/operator-overloaded/2181.html

Operator Overloading www.computing.net/answers/programming/operator-overloading/12359.html