• We should now be fully online following an overnight outage. Apologies for any inconvenience, we do not expect there to be any further issues.

Overriding == in java... how?

purpledemon

Senior member
Jun 21, 2001
291
0
0

The capability of allowing user defined operators is not part of the Java language defined by Sun Microsystems

however ...

you could use or write a preprocessor

jfront
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
What a peice of crap. How the hell does Java deterine if two objects are equivelent or not?
 

OneOfTheseDays

Diamond Member
Jan 15, 2000
7,052
0
0
ha, i was thinking the exact opposite when I started programming in C++, after having done extensive Java coding. As others have said, just use the CompareTo() function that is provided in java. Once you get used to java though, you'll never want to go back to C or C++, the garbage collector in Java is a godsend.
 

manly

Lifer
Jan 25, 2000
13,292
4,064
136
Originally posted by: notfred
What a peice of crap. How the hell does Java deterine if two objects are equivelent or not?
In Java, you use Object.equals() rather than the == operator for reference types. So the only question is if you need to override it (and also if you override Object.hashCode() as well).

For most of us, not being able to overload operators is not a big deal. The argument goes that operator overloading was more misused than useful.

As far as Sun is concerned, operator overloading is still not an important language feature. Other currently missing features have been deemed important enough for Java 1.5 (Tiger) though.
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
Originally posted by: manly
Originally posted by: notfred
What a peice of crap. How the hell does Java deterine if two objects are equivelent or not?
In Java, you use Object.equals() rather than the == operator for reference types. So the only question is if you need to override it (and also if you override Object.hashCode() as well).

For most of us, not being able to overload operators is not a big deal. The argument goes that operator overloading was more misused than useful.

As far as Sun is concerned, operator overloading is still not an important language feature. Other currently missing features have been deemed important enough for Java 1.5 (Tiger) though.

Although I appreciate that C# and C++ have op overloading, I agree with you. The use of op overloads in C++ always made things difficult to debug when a simple comparison operation threw an exception. I've always been of the opinion that being explicit is better than the taste of syntactic sugar, and abstracting yourself from such details is detrimental; they leak.

.NET does paint a prettier picture, however. C# supports op overloading, but VB.NET and friends do not. VB.NET can still call the emitted op overload methods: op_Addition for +, op_Subtraction for -, op_Equality for ==, etc. This is pretty absurd as it negates the syntactic sugar afforded by op overloading in the first place. Fortunately there are guidelines in place that help mitigate this a bit. In order to pass tests like FxCop for class libraries you have to provide a corresponding public method in place of the op overload: Add for +, Subtract for -, Equals for ==, etc..

Why do I always post a longer response than I anticipated?
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Originally posted by: Descartes
Originally posted by: manly
Originally posted by: notfred
What a peice of crap. How the hell does Java deterine if two objects are equivelent or not?
In Java, you use Object.equals() rather than the == operator for reference types. So the only question is if you need to override it (and also if you override Object.hashCode() as well).

For most of us, not being able to overload operators is not a big deal. The argument goes that operator overloading was more misused than useful.

As far as Sun is concerned, operator overloading is still not an important language feature. Other currently missing features have been deemed important enough for Java 1.5 (Tiger) though.

Although I appreciate that C# and C++ have op overloading, I agree with you. The use of op overloads in C++ always made things difficult to debug when a simple comparison operation threw an exception. I've always been of the opinion that being explicit is better than the taste of syntactic sugar, and abstracting yourself from such details is detrimental; they leak.

.NET does paint a prettier picture, however. C# supports op overloading, but VB.NET and friends do not. VB.NET can still call the emitted op overload methods: op_Addition for +, op_Subtraction for -, op_Equality for ==, etc. This is pretty absurd as it negates the syntactic sugar afforded by op overloading in the first place. Fortunately there are guidelines in place that help mitigate this a bit. In order to pass tests like FxCop for class libraries you have to provide a corresponding public method in place of the op overload: Add for +, Subtract for -, Equals for ==, etc..

Why do I always post a longer response than I anticipated?

It's all that MS in your blood, things you make bloat up easily ;) (seriously, joking, joking :D)
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
Originally posted by: BingBongWongFooey
Originally posted by: Descartes
Originally posted by: manly
Originally posted by: notfred
What a peice of crap. How the hell does Java deterine if two objects are equivelent or not?
In Java, you use Object.equals() rather than the == operator for reference types. So the only question is if you need to override it (and also if you override Object.hashCode() as well).

For most of us, not being able to overload operators is not a big deal. The argument goes that operator overloading was more misused than useful.

As far as Sun is concerned, operator overloading is still not an important language feature. Other currently missing features have been deemed important enough for Java 1.5 (Tiger) though.

Although I appreciate that C# and C++ have op overloading, I agree with you. The use of op overloads in C++ always made things difficult to debug when a simple comparison operation threw an exception. I've always been of the opinion that being explicit is better than the taste of syntactic sugar, and abstracting yourself from such details is detrimental; they leak.

.NET does paint a prettier picture, however. C# supports op overloading, but VB.NET and friends do not. VB.NET can still call the emitted op overload methods: op_Addition for +, op_Subtraction for -, op_Equality for ==, etc. This is pretty absurd as it negates the syntactic sugar afforded by op overloading in the first place. Fortunately there are guidelines in place that help mitigate this a bit. In order to pass tests like FxCop for class libraries you have to provide a corresponding public method in place of the op overload: Add for +, Subtract for -, Equals for ==, etc..

Why do I always post a longer response than I anticipated?

It's all that MS in your blood, things you make bloat up easily ;) (seriously, joking, joking :D)

:D
 

igowerf

Diamond Member
Jun 27, 2000
7,697
1
76
It took me forever to find why some of my java programs didn't work. I kept comparing strings using ==.