Roam
Roam

Reputation: 4949

Classes that intern in Java

Which Java classes are interning?

String is.

The descendants of Number don't seem to be interning.

Are there any others in the java.lang or in APIs that are interning? i.e., bringing an already existing object from the pool when that value of the class is called for multiple times?

Upvotes: 1

Views: 276

Answers (1)

Makoto
Makoto

Reputation: 106390

All of the wrapper classes have an internal cache mechanism. They cache values that range from -128 to 127.

A couple of special cases to note:

  • Character is special in that, since a char < 0 has no meaning, it caches from 0 to 128.

  • Boolean creates static constants of true and false, so it is always a cached wrapper.

Admittedly this isn't in the same sense as a String (since that's VM interning as opposed to runtime), but there does exist some small bit of caching for those wrapper classes.

Upvotes: 2

Related Questions