[NTLUG:Discuss] Java/Linux versus school teacher.

steve sjbaker1 at airmail.net
Mon Mar 19 12:42:30 CDT 2007


Chris Albertson wrote:
> I don't like the way the teacher is approaching the set of questions.
> You are exactly right in your analysis below. I'm not really sure why 
> your test program decided to return true. I certainly would not trust 
> that particular implementation of string comparison.

No, no, no!  We all agree that '==' doesn't do a string compare.
It's very obvious that the question was designed to test whether
the students know that or not.

The issue is whether when you write:

   String x = "Hello" ;
   String y = "Hello" ;

...are you GUARANTEED to get two different String objects each
containing the characters H-e-l-l-o or is it OK for the compiler
to make both references point to the same string?  In the the
first case (x==y) is false - but in the latter case, it could be
true (and indeed it is in Linux) even though no string compare
was performed.

> The teacher really should not be testing in this way though, in my 
> opinion. String.equals is a much better, standard, and reliable method 
> for comparing strings.
> So, x.equals(y) is much better... provided x!=null of course.

Yeah - and the kids know that.  The problem is that if you DO use '=='
are you guaranteed to get 'false'? (and the answer appears to be 'No').




More information about the Discuss mailing list