Are all immutable objects re-usable?
Asked Answered
L

3

5

From the effective Java book it states that "An object can always be reused if it is immutable".

String s = "shane";
String p = "shane";

This version uses a single String instance, rather than creating a new one each time it is executed. Furthermore, it is guaranteed that the object will be reused by any other code running in the same virtual machine that happens to contain the same string literal.

What about the below final class which is also immutable?. Can the Point Object be re-used?.

public final class Point {
  private final int x, y;

  public Point(int x, int y) {
   this.x = x;
   this.y = y;
  }

  public int getX() { return x; }
  public int getY() { return y;
 }

Can anyone provide me an example of the above immutable class where its object/instance can be re-used?. I am just confused on how the re-usability would occur?.

I am able to relate with String and Integer Classes, but not with user defined classes.

Lacerated answered 23/9, 2013 at 3:47 Comment(2)
Immutable class == immutable ObjectVisualize
It can be reused,but that's only guaranteed for the string class.Wintertime
H
13

It "can" be reused, in that you could use the same object in multiple places and it would be fine. But it won't be, automatically. The JVM itself manges reuse Integer objects for the range -128 - 127

Integers caching in Java

"intern"ed strings (including literals) similarly are managed by the JVM. The closest to automatic reuse you could have here would be to make the constructor private, and create a factory method:

 Point.create(int x, int y)

And have the implementation maintain a cache of objects that you'd like to reuse (like Integers effectively cache -128 to 127) But you'll have to do the work yourself.

Edit:

You'd basically have:

 private static final Map<Pair<Integer, Integer>, Point> cache = new HashMap<>();

 public Point create(int x, int y) {
   Pair<Integer, Integer> key = Pair.of(x, y);
   if (cache.containsKey(key)) {
     return cache.get(key);
   }
   Point p = new Point(x, y);
   cache.put(key, p);
   return p;
 }

Edit: Alternatively, add hashCode() and equals() to the Point class, and just use a HashSet. Would be simpler.

Handcuff answered 23/9, 2013 at 3:52 Comment(4)
How could that, even if i make use of static factory method... i am still creating a new Point() every time i invoke Point.create(int x, int y)... can you add more to your post to make little bit more clear.Lacerated
@Lacerated Added an edit with an example. You don't always create a new Point() - the point of the factory method is that you can either create a new point, or return a previously created point if it's a duplicate.Handcuff
+1. Other examples of small-scale caching of immutable objects in the JVM include BigDecimal.ZERO/ONE/TEN, Collections.EMPTY_LIST/EMPTY_SET/EMPTY_MAP, and Boolean.TRUE/FALSE. The common factor is that there's no attempt to cache all values, nor to force caching, but only to ease the sharing of a few values that are likely to be used many times in a single program.Economy
Strictly speaking, you should be using a ConcurrentHashMap or something else that is thread safe. As a talking point, this code is OK, but cutting and pasting it to use in anger would cause trouble later.Faught
C
0

Re usable simply means to change the "reference" variable value. e.g. an int is can be reused and its value changed a data type is a little different the reference variable is re-initiated for example using the "new" instane e.g. myframe=new JFrame() variables declared "final" are a "constant" and are mutable.

The class above itself requires its reference variable at initiation to be declared "final" to be mutable although its contents is effectively mutable, the difficulty is the definition of context of which (variable or class definition) part is the mutable.

Causalgia answered 23/9, 2013 at 4:1 Comment(0)
G
0

Immutability means when an object is created its state at the creation time is going to stay through out its life. And yes, the class you showed and object of that class is immutable, as you are initialing states in constructor and there are no setters.

About the re-use: yes you can reuse the same object over and over where an object of type Point is required, but for that purpose you have to hold on to an object once it's created for that. As @James suggested, you can use a factory for object creation and that factory can decide if it needs to create a new object or use an existing one when you ask for a Point object.

Guipure answered 23/9, 2013 at 4:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.