[NTLUG:Discuss] Java/Linux versus school teacher.
    Patrick R. Michaud 
    pmichaud at pobox.com
       
    Mon Mar 19 11:52:46 CDT 2007
    
    
  
On Mon, Mar 19, 2007 at 11:42:30AM -0600, steve wrote:
> 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?  
It's okay for the compiler to make both references point to the
same string [1].  Also, from "Core Java Technologies Technical 
Tips" [2]:
    The Java platform creates an internal pool for string 
    literals and constants. String literals and constants that 
    have the exact same char values and length will exist exactly 
    once in the pool. Comparisons of String literals and constants 
    with the same char values will always be equal.
So, this would seem to indicate that for the way the question
was phrased, one should always get the value "(a) true" 
(which is the way I remember it being).  :-) :-)
[1]  http://wsjoung.wordpress.com/2006/11/26/string-constant-pool/
[2]  http://java.sun.com/mailers/techtips/corejava/2006/tt0822.html
Pm
    
    
More information about the Discuss
mailing list