fred,
I can tell you are from a C background.
Thinking about pointers when you think about Java can lead you down a trail of tears.
When someone codes String constants in their code (a string constant is anything between the " marks), then the java runtime will produce a single instance of that string in memory. Since Strings are immutable, this is a safe thing to do. So if you do your assignments like this:
This will produce two trues.
a==b Always compares if the objects are the same object. This is true in this case, because both a and b are 'referring' (or if you must 'pointing') to the same single instance of that string constant "Hello" that your runtime has set aside in memory. (They contain the same address). No new string needs to be constructed (the new keyword is unnecessary) because this String constant is already there to be used.
a.equals(b) is also true because "Hello" does indeed equal "Hello" (ie: the words are the same)
If, though, you do this:
This will produce false, then true.
When you use new keyword you are explicity telling the runtime to create a separate String object. You are using the following constructor String(String s). The runtime uses the previously set aside string constant "Hello", to produce a second and *Different* string object, also containing the
word "Hello". It does this again with the b String.
So when you compare a==b -> These are two different object references with two different addresses in them. So they do not point to the same object.
But of course, "Hello" is still the same word as "Hello", so the a.equals(b) still produces true.
So as an interesting aside... all other things being equal (programming requirements don't force you to make new strings each time).. the first code uses about 1/3 the resources that the second code does. In the first code, only one String object is made, (which two references 'point' to), and in the second code, there are three String objects.
Apologies for not always saying uppercase String when I should have.