[5150] in java-interest

home help back first fref pref prev next nref lref last post

Re: Strings.substring() and the == operator

daemon@ATHENA.MIT.EDU (Christophe Meessen)
Tue Jan 30 10:10:59 1996

Date: Tue, 30 Jan 1996 14:45:30 +0100
To: Arthur.Vanhoff@Eng.Sun.COM (Arthur van Hoff), mikep@exmachina.com
From: Christophe Meessen <meessen@cppm.in2p3.fr>
Cc: java-interest@java.Eng.Sun.COM

At 10:48 29/01/96 -0800, Arthur van Hoff wrote:
>
>> >On Sun, 28 Jan 1996, Walter Szewelanczyk wrote:
>> >
>> >"==" is a test for identity not for equality.  
>> >You should always use equals() instead.
>
>But! There is a reason for this. Equality and Identity are both
>useful. You need both in a system where you have value objects
>that are passed by value as well as object that are passed
>by reference. 

What is the most frequent and most intuitive usage of == ?
Testing equality.

Testing identity is specific for referenced value as you say.
So instead of defining an equals() method it would have been more 
appropriate to define a identical() or sameInstanceReference().

The semantic of the == operator is a bit twisted in java.

But there is another possible explanation to this choice of java.
We may want to compare user defined objects. This comparision method 
may have to be defined by the user so it can be, for instance, optimised.
Since the == operator can't be redifined (overloaded/overrided) it was 
then logical (but not optimal) to define an equals() function
and restrict the == operator to reference comparision.

Prohibiting operator overloading has it's benefits but here we reach one
of it's limits.

In my opinion the choice made in Java is the less bad one.
Bien cordialement,

Ch.Meessen

-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post