[Bluej-discuss] confusion over = (and == ?)

Ian Barland ibarland at radford.edu
Fri Nov 30 19:28:14 GMT 2007


Carl Alphonce writes:
>
> "==" is not overloaded.  "==" does the same thing in both cases.
>
I'd argue that == is overloaded, and even mention in CS1:
  == : int, int --> boolean
  == : double, double --> boolean
  == : Object, Object --> boolean    (once we have talked about references)

   / : double, double --> double
   / : int, int --> int              // named "quotient" in some languages

Yes, like any good overloaded method, each version of == does a similar task
(unlike "/").

[True, these different overloaded methods for == might each compile to the
same op code, but at the high-level of Java syntax == must be overloaded so
that the auto-casting rules for 0==0.0, etc, can be invoked.  Also note that
Double.NaN == Double.NaN, so the == for doubles requires an extra check
which the other overloaded versions don't.]


Is it worth *teaching* that even operators have signatures?  I've found so;
it helps reinforce that "==" returns a result which is usable, it also helps
explain why "/" seems to give different answers, and overall it just helps
unify the whole idea of calling functions.  (And while you might sigh that
it's a whole second syntax for calling a function, at least it's not as
(b)ad hoc as the syntax for casting primitives.)

--Ian B.



More information about the bluej-discuss mailing list