Skip to main content

Add === operator for a.equals(b).

59 replies [Last post]

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2003-06-16

Sorry guys, but we are adding entropy to the system.
In my opinion Sun has to reconsider operator overloading !


Joined: 2004-01-01

I agree that "a.equals(b)" is so often used that it deserves a special operator. "===" looks like a good suggestion to me.

I also think that operator overloading should not be condemned altogether.

It is certainly a bad idea to allow overlaoding of standard operators (because this can result in unmaintainable code).

But why allow to use any functions that takes two parameters instead of a binary operator?

For example we could write:

boolean eq (Object a, Object a) {
return (a == b || (a != null && a.equals(b));


if (obj1 eq obj2) ...

Or we can create functions for division and addition of BigDecimals:

BigDecimal a = (b div c) plus (d div e):

This is much more readable than

a = add(div(b,c),div(d,e));

We could get the best of both worlds: noone can fool around with standard operators and we get the syntactical sugar of being able to use operators.

Joined: 2004-01-28

I like your idea. I wouldn't really call it operator overloading, but more like an operator function - they can be declared like a method, must return a type, and say have an annotation like @Operator - that way we make the compile rewrite [b]a div b[/b] to be [b]div(a,b)[/b]. My guess they would also have to be static. I didn't like the C++ use of some overloading as being member functions, and some being friend functions - need one or the other, but not both, because it caused me much confussion.

Joined: 2003-06-10

> (a == b || (a != null && a.equals(b))

Well, we have such a code in our project as well, but did not have a problem to create a nice utility method. What's wrong with writing:

if (CompareUtils.areEqual(a, b)) {

IMHO, this is clean and VERY easy to read. Much more clean, than the default unsymmetric equals-method.

Joined: 2003-06-10

Going further this way, we can have

a = MathUtils.add(MathUtils.div(b,c),MathUtils.div(d,e));

instead of

a = b/c + d/e;

(that evil, unsymmetric '/' in java...)

Nobody says that we cannot make utility method for areEqual. Trick is, that this particular combination is used so often that it might deserve special operator.

Joined: 2004-10-14

Exactly. Why should everybody write the same utility class?

It would reduce the number of NullPointerExceptions, too, for when one does not write a utility class for comparison because one thought something would certainly never be null ... and was wrong.


Joined: 2003-06-10


What should be the value?

Joined: 2004-03-04


my favorite one ;)

Joined: 2003-06-10

!== would mean opposite of it ?

Other funny operators to consider:

Max/min from gcc
a a >? b meaning max(a,b)

a a >?= b meaning a=max(a,b)

Not-so-ternary operator
a ?: b meaning (a!=null)?a:b
like (str ?: "empty")