[Ohrrpgce] thoughts about slice handles

S'orlok Reaves sorlok_reaves at yahoo.com
Tue Jan 20 00:16:59 PST 2009


>  variable (a, b)
>  a := "A"
>  b := a
>  b += "B"

For the record, here's what Java does:
1) a is "A"
2) b is "A" (same reference)
3) b is "AB", and a is still "A"

That's because Java enforces two properties about strings, at all times:
A) Two strings with the same value MUST have the same reference.
B) Any mutating operation on a string will return a new string reference.

I've heard that (A) is done for performance reasons, and (B) is done to keep the language simple to use. In my experience, most students find this intuitive. The only confusion is:

a.toUpperCase(); 

...will return a new string (it won't operate on a), so it needs to be stored:
a = a.toUpperCase(); 

-->Seth




      



More information about the Ohrrpgce mailing list