Are all immutable objects reusable?

From an 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 , and does not create a new one each time it is executed. In addition, it is guaranteed that the object will be reusable by any other code running on the same virtual machine that contains the same string literal.

What about the lower final class, which is also unchanged ?. Is it possible to reuse a Point object?

 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 someone provide me the example of the above immutable class , where its object / instance can be reused ?. I'm just confused about how reuse will happen ?.

I can bind to String and Integer classes, but not to user-defined classes.

+4
source share
3 answers

It can be "reused" because you can use the same object in several places, and everything will be fine. But this will not happen, automatically. The JVM itself manages the reuse of Integer objects for the range -128 - 127

Java Integer Caching

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

  Point.create(int x, int y) 

And the implementation has a cache of objects that you want to reuse (for example, whole elements effectively cache -128 to 127). But you have to do this work yourself.

Edit:

Basically you:

  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: Also, add hashCode () and equals () to the Point class and just use a HashSet. It would be easier.

+11
source

Re useable simply means changing the value of the reference variable. for example, int can be reused and its value changed; the data type is slightly different; the reference variable is re-initiated, for example, using a "new" setting, for example. myframe = new JFrame () variables declared "final" are "constants" and are mutable.

The class above itself requires that its reference variable be declared β€œfinal” upon initiation in order to be mutable, although its contents are effectively mutable, the difficulty is determining the context of which the variable is a part (variable or class).

0
source

Invariability means that when an object is created, its state at the time of creation will remain throughout its life. And yes, the class you showed and the object of this class are immutable, since you initialize the states in the constructor, and there are no installers.

About reuse: yes, you can reuse the same object over and over again when you need an object of type Point, but for this purpose you should hold on to the object as soon as it was created for this. As @James suggested, you can use a factory to create an object, and this factory can decide whether it needs to create a new object or use an existing one when you request a Point object.

0
source

All Articles