Bear Bibeault wrote:No, it has nothing at all to do with String immutability. It has to do with when objects are created and how references are handled. The first example could use any class and the result would be the same.
Once again in the longer (which I didn't look at closely) you are replacing the objects with new one, while in the second you are not creating new objects but changing the properties of the existing object.
Taking this out of JSP (again, Java code in a JSP is huge no-no), the same behavior would be observed calling Java methods.
Ravi Khadgi wrote:
Bear Bibeault wrote:No, it has nothing at all to do with String immutability.
Agree with you. However, being an immutable class, String objects are created and handled differently than other mutable objects.
If you do String oneString = anotherString , it would create a new object referenced by oneString variable.
if you do AdamClass oneAdam = anotherAdam , it would not create a new AdamClass object.
Adam Nagy wrote:
Here is the link for the blog:
http://nagyadam2092.blogspot.hu/
Basically every time you are making expression's like this:
blabla = "blabla";
x = 10;
every time a new object is created. So it's like you are using the new String(".."); expression, but it's not showed.
So when you're about to pass a String or an Integer object to a function , you could get the value of it,
but if you want to change it, it will be not changed, because the "=" expression is making a new object.
Ivan Jozsef Balazs wrote: got into the string pool, so executing
does not make a new object.
Basically every time you are making expression's like this:
blabla = "blabla";
x = 10;
every time a new object is created.
Ivan Jozsef Balazs wrote:
Basically every time you are making expression's like this:
blabla = "blabla";
x = 10;
every time a new object is created.
This is simply false and I was not willing to let it stand.