Another bizarre consequence of this philosophy becomes evident when we
consider equality. Java has a built-in equality operator
However, if we write
Complex u = Complex(2,3); Complex v = Complex(2,3); System.out.println(u==v); // Standard way to print.we'll see the answer
false. Surely this violates the principle of least astonishment, a principle which, according to [Plum], should be obeyed by language designers, and which is broken
... when a ``natural'' or ``abbreviated'' language construction leads the user to induce a model of underlying action different from what is actually performed by the machine.
== can indeed mislead is also pointed out by Lemay and Perkins
[Lemay and Perkins].
Java has a built-in object type
String for storing sequences of
characters. If you use
== to compare two strings:
String a = "a string"; String b = "a string"; System.out.println( a==b );you get the answer
false, even if they contain the same characters. To circumvent this, it is suggested that the method
equals, should be defined especially with the String type:
System.out.println( a.equals(b) );