| Frank Swarbrick 2006-03-09, 6:55 pm |
| Oliver Wong<owong@castortech.com> 03/09/06 7:51 AM >>>
>[post slightly re-ordered]
>
>"Frank Swarbrick" <Frank.Swarbrick@efirstbank.com> wrote in message
>news:47972lFeicknU1@individual.net...
>
> As Richard said, == compares references ("pointer addresses" if you
want
>to think of that way, though in a technical/pedantic manner, that's not
>strictly correct), while .equals() invokes the "equals" method, which can
be
>programmed to do anything you want. In the case of Strings, "equals"
>compares the contents of the strings for equality (in a case-sensitive
>manner).
>
> So in Java, you'd probably be writing:
>
>if ("one".equals(myString)) {
> one();
>} else if ("two".equals(myString)) {
> two();
>}
>/*etc.*/
>
>
>
> In the special case of Java, I'd say "yes". The reason is that Java
>already made the mistake (well, it's a mistake in my opinion) of sometimes
>treating Strings like primitives, and other times treating them like
>Objects. The above kind of construct perpetuates this illusion that Strings
>are like primitives.
>
> So "normally", I'd say "no", and that the above construct would detract
>from the (mathematical) elegance of the language. However, since Java has
>already went down the road of treating Strings like primitive, the above
>construct won't add much more harm, and does provide some convenience to
the
>programmer, so my final answer is "Yes, that'd be nice."
I understand the reasoning behind '==' comparing the references. I just
don't agree with it. Meaning, I think to most people just starting
programming, with little understanding of a "reference object" versus a
primative type, would assume that "if (string1 == string2)" would be
comparing the "values" of the strings, and whether or not the two strings
refer to the same object. Personally, I'd treat == of two objects as an
alias for the "equals" method and have something else be used for a
reference comparison. Perhaps "if (@string1 == @string2)" or something...
As a matter of fact, this is pretty much how C# works
if (s1 == s2): compares the values of the two strings
if ((object) s1 == (object) s2): compares the "references" of the two
strings
Not sure if this is true only for strings or for all objects...
>
> I agree. I'd like for the default behaviour to be a break, and if a
>fall-through is desired, for that to be explicitly said. E.g.
>
>switch (myInteger) {
> case 1:
> handleOne();
> case 2:
> fallthrough;
> case 3:
> fallthrough;
> case 4:
> handleTwoToFourButNotFive();
> fallthrough;
> case 5:
> handleTwoToFive();
> default:
> handleDefaultCase();
>}
Indeed.
---
Frank Swarbrick
Senior Developer/Analyst - Mainframe Applications
FirstBank Data Corporation - Lakewood, CO USA
|