The Department of Computer Science & Engineering |
STUART C. SHAPIRO: CSE
116 B
|
Try adding something twice and removing it once. Is it there?
Note: BeanShell, but not compiler, allows HashSets of primitive values.
HashSet
s of chars and to
CharBag
s.
this
except for some small change.
For example Strings
are immutable sequences of char
s,
while StringBuffers
are mutable sequences of char
s.
Compare String
's concat(String str)
to
StringBuffer
's append(String str)
, by trying
in the BeanShell
s1 = new String("Original"); sb1 = new StringBuffer("Original"); s2 = s1; sb2 = sb1; s1 = s1.concat(" and modified"); sb1 = sb1.append(" and modified"); print(s2); print(sb2);
Mutators need not return this
, but if they do, they
can be composed (see text, page 68).
Mutable objects avoid creating extra objects.
Mutators may cause a variable to "change", even though it has not been
referenced. (See sb2
above.)