Situation: Girlfriend's Java teacher says the following statements are equivalent. At the same time, a Java book recommends the latter form.
String a = new String("Hi");
String b = "Hi";
Then a.equals(b), and the world is fine.
However, my Java text and C knowledge gravely inform me that since a==b
as well as a.equals(b)
, if a
is changed (Not likely in a String
, but what if it gets mutated anyhow, as it is probably possible to pass the address somehow that even the JVM won't stop) then b
will also be changed. As such, it could be dangerous to assume that b.equals("Hi")
.
Any insight? Is the declaration for b
really safe, or just a lot lighter on the RAM?
I am not asking about what the difference is. I know that b
probably references the same object created in a
.