string - Java Immutable Objects -


i learning concept of immutability.

i understand immutable objects cannot change values once object created.

but didn't understand following uses of immutable objects.

they are

  • are automatically thread-safe , have no synchronization issues. how ? proof ?
  • do not need copy constructor. how ? example ?
  • do not need implementation of clone how ? example ?
  • do not need copied defensively when used field how ? example ?
  • always have "failure atomicity" (a term used joshua bloch) : if immutable object throws exception, it's never left in undesirable or indeterminate state. how ? example ?

could please explain each of these points in detail examples supporting ?

thanks.

..are automatically thread-safe , have no synchronization issues

concurrency problems happen when 2 different threads modify state of same object. immutable objects can't modified, no problems.

example: string. 2 threads can passed same string without worry since neither can mutate in way.

do not need copy constructor

... because copy way mutate it. 1 common design pattern immutable objects every "modification" operation make copy , perform operation on new object.

copy constructors used on objects want change without affecting original. case (by definition) immutable objects.

in case of string, methods , + operator return new strings.

do not need implementation of clone

see above.

do not need copied defensively when used field

once upon time did silly. had set of enums in list:

private static final list<status> validstatuses;  static {   validstatuses = new arraylist<status>();   validstates.add(status.open);   validstates.add(status.reopened);   validstates.add(status.closed); } 

this list returned method:

public static list<status> getallstatuses() {   return validstates; } 

i retrieved list wanted show open states in interface:

list<status> statuses = status.getallstatuses(); statuses.remove(status.closed); 

great, worked! wait, status lists showing 2 -- after page refresh! happened? modified static object. oops.

i have used defensive copying on return object of getallstatuses. or, use guava's immutablelist in first place:

private static final list<status> validstatuses =     immutablelist.of(status.open, status.reopened, status.closed); 

then when did dumb:

list<status> statuses = status.getallstatuses(); statuses.remove(status.closed);  // exception! 

always have "failure atomicity" (a term used joshua bloch) : if immutable object throws exception, it's never left in undesirable or indeterminate state.

because class can never modified, states emitted modification whole, qualified objects (because cannot change, must in qualified state useful). exception not emit new object , therefore can never have undesirable or indeterminate state.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -