Wednesday, March 21, 2012

Challenge 048

I       s1 == s2              false
II      s1.equals(s2)       true
III     s3.equals(s2)       true

Statement I is false because == is an operator that means the two objects have equality to one another. However, the objects are not the same (s1 and s2 are two separate unequal objects).

Statement II is true because equals refers to the values of the two objects being equal. This is the case because they both have the String value "crab, they are simply stated in two different ways

Statement III is true because s3(= s1) does equal s2(new String("crab"). The values are the same (String "crab"). However if it were stated: s3 == s2, it would have been false because s3 and s2 are two different objects.


The following website explains the == operator:
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html

1 comment:

  1. Hi Ryan,

    There's this part of your explanation that's spot on:

    "However if it were stated: s3 == s2, it would have been false because s3 and s2 are two different objects."

    and then there's this explanation that's not technically accurate explaining the same kind of situation:

    "Statement I is false because == is an operator that means the two objects have equality to one another."

    I would here ask equality in terms of what? But you understand that were comparing object instances in computer memory here, so I'll call it complete.

    200 XPs awarded.

    ReplyDelete