in reply to Perl and C don't always mix well

Going back to Java one day, i found myself trying to check for the equality of a string like so:
if (foo eq "bar") . . .
Then i realized, "Hang on! Java doesn't have eq, it only has ==" . . . So:
if (foo == "bar") . . .
And wondered for a long amount time (much longer than i care to admit) why it was always, always, always false . . .

jeffa


Answer:

Because i was comparing address spaces. The proper way to compare strings in Java is with the equals() method from the String class.

Replies are listed 'Best First'.
Re (tilly) 2: Perl and C don't always mix well
by tilly (Archbishop) on Nov 01, 2001 at 08:30 UTC
    Random tip that works well across several languages.

    Turn that test around. If you write it:

    if (5 == $number) { # ... }
    then you are automatically told your mistake when you write
    if (5 = $number) { # ... }
    by accident. By contrast you are rather less happy if you write in the naive order and wound up with:
    if ($number = 5) { # ... }
    which winds up being true rather more often than you would like...