Tespa42
Tespa42

Reputation: 577

Does Java optimize immutable objects?

Java strings are immutable, and instantiating multiple Strings with the same values returns the same object pointer. (Is there a term for this? "pooling" seems to fit, but that already refers to doing caching to save time by doing fewer instantiations.)

Does Java also do this (the thing without a term) with other (user-defined) classes that are immutable? Can Java even detect that a class is immutable, or is this something unique to the string class?

Upvotes: 11

Views: 1433

Answers (5)

Sean Landsman
Sean Landsman

Reputation: 7179

As others here have said this process with Strings is known as interning.

Its worth mentioning that the behaviour of Strings with the same literal values being the same object may or may not be true in Java 7. From 7 onwards:

In JDK 7, interned strings are no longer allocated in the permanent generation of the Java heap, but are instead allocated in the main part of the Java heap (known as the young and old generations), along with the other objects created by the application. This change will result in more data residing in the main Java heap, and less data in the permanent generation, and thus may require heap sizes to be adjusted. Most applications will see only relatively small differences in heap usage due to this change, but larger applications that load many classes or make heavy use of the String.intern() method will see more significant differences.

Take a look at Java SE 7 RFE for the full details on this.

With regards to your own immutable objects Java doesnt do anything special with them - it doesnt know that they're immutable. It may inline methods a little more than otherwise if it can detect that its worthwhile/possible, but as far at the compiler and JVM are concerned they're just another object.

Upvotes: 3

The term you are lookig is itering. Java optimize strings "automatically", during compilation and give the developer possibility to do it on runtime. (The details about what is optimized when depend on JVM version.)

As far it goes for immutable objects. I do not think that Java support any type of mechanism that will resolve same instace. String type is not exeption of this rule.

Reason why, is that you have to use operator new to create a instance. If you use new to create string instance, you will always get two different objects.

The intering is avaiable only for String type. But the concept is free, you can add to your immutable class such method and write an compled method that will do the same thing.

Upvotes: 1

codeMan
codeMan

Reputation: 5758

String Interning is unique to String class only. I suppose that JVM does not apply these rules for a user defined classes.

Upvotes: 0

Bernd Elkemann
Bernd Elkemann

Reputation: 23550

String interning. Wikipedia: String Interning

Upvotes: 0

Brian Agnew
Brian Agnew

Reputation: 272257

Wrt. Strings, the word you're looking for is interning.

Java won't do this for your own immutable objects. It does have cached versions of boxed primitives, though. See this article on wrapper class caching for more info.

Upvotes: 8

Related Questions