[NTLUG:Discuss] Java/Linux versus school teacher.
Chris Albertson
alb at chrisalbertson.com
Mon Mar 19 10:28:36 CDT 2007
Ack. Spell checker bit me.
I didn't mean privative... I meant primitive. (I didn't even know
privative was a word.)
Chris
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.
> 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.
>
> I don't know what to do in your instance. Maybe run the test program for
> the teacher and demonstrate the unpredictability of this method? The
> correct answer is not in the list of choices. The "best" answer is
> indeed b though.
> As far as java being a crappy language because of it's ability to
> produce inconsistent results... this is a weird case. String is not a
> privative. It's a class in and of itself. Using == on it is just never a
> good idea.
>
> Chris
>
> steve wrote:
>
>> Do we have any Java/Linux guru's in the house?
>>
>> My son is learning Java in high school - inevitably they use
>> Windows machines - but it's Java - which is portable - so it
>> doesn't matter a whole lot. So my kid can use Linux to do
>> his homework without problems.
>>
>> ...mostly...
>>
>> The other day, they had a Java Exam. The first two questions
>> go like this:
>>
>> Assume x and y are String variables with x="Hello" and y=null.
>>
>> 1) The result of (x==y) is
>> a) true
>> b) false
>> c) a syntax error
>> d) a run-time error
>> e) x being set to the value null.
>>
>> I think we can all agree that the answer is (b)false.
>>
>> 2) It the operation y="Hello"; is performed then the result
>> of (x==y) is:
>> a) true
>> b) false
>> c) x and y becoming aliases
>> d) x being set to the value null
>> e) a run-time error
>>
>> Question 2 is a problem. My son said "(a) true" but the
>> teacher says that the answer is "(b)false" - and whilst
>> I'm not a Java expert (I'm a C++ person) I can see why. Even
>> though both x and y are set to "Hello", they don't refer to
>> the same String object and the '==' operator doesn't compare
>> the contents of the objects - it only tests whether the same
>> object is being referenced on both sides. (At least according
>> to "Java2 for Dummies" and my C++ intuition).
>>
>> When my son makes mistakes like that, I encourage him to write
>> a little program to convince himself. The trouble is that when
>> we tested it by writing a little Java program and running it
>> under Linux:
>>
>> String x="Hello";
>> String y="Hello";
>> if ( x==y )
>> println ( "true" ) ;
>> else
>> println ( "false" ) ;
>>
>> The answer comes out "true"...oh-oh!
>>
>> This suggests that some kind of optimisation is going on inside
>> the Java compiler and the two instances of the "Hello" string are
>> being combined into one single object or something.
>>
>> Clearly then the teacher is wrong and the correct answer is "either
>> true or false depending on the implementation"...but I'm having
>> a hard time believing that Java is that ill-specified. Is it in
>> fact the case that Java under Windows would also have reported
>> 'true'?
>>
>> So should I complain to the teacher? Stop using Linux for this
>> stuff? (over my cold, dead body!)...Or is there something subtle
>> that I'm missing here?
>>
>>
>> Thanks!
>>
>> _______________________________________________
>> http://www.ntlug.org/mailman/listinfo/discuss
>>
>>
>
> _______________________________________________
> http://www.ntlug.org/mailman/listinfo/discuss
>
More information about the Discuss
mailing list