2

I know that Strings/literals are optimized - they are stored in NoHeap PermanentGeneration - Interned Strings, so if you create two same literals, they would point to the same address in memory.

Interned Strings (String Table) The Java Language Specification requires that identical string literals, that contain the same sequence of Unicode code points, must refer to the same instance of String. In addition if String.intern() is called on an instance of String a reference must be returned that would be identical to the reference return if the string was a literal. The following therefore holds true: ("j" + "v" + "m").intern() == "jvm"

What about other types - are they stored in some JVM area? I heard that Integers are cached somehow - but in JVM or in a static manner inside Integer class? Are all Integers are cached - from Integer.min_value to max_value? Are simple types like int are also cached? What about other types like BigDecimal, Long, char etc?

Are big types like Long, Integer cached in the same place as long and int? JVM or where?

  • Look at the documentation for `Integer.valueOf(int)` etc. You should find the answers. – Paul Boddington May 03 '15 at 14:58
  • `Integer`s are cached for values from -128 to 127 (http://stackoverflow.com/questions/3131136/integers-caching-in-java), `int`, `long` and `char` are primitive types so don't need to be cached at all – Alex Salauyou May 03 '15 at 15:09
  • @SashaSalauyou Not sure if the positive upper limit of `128` is correct or documented. I could be wrong but it's only the negative limit that is guaranteed. – Chetan Kinger May 03 '15 at 15:10
  • 1
    In src.zip, java/lang/Integer.java, I see the explicit implementation of an IntegerCache class. So it looks like Integer is explicitly cached, whereas String interning is a JVM feature. – Nayuki May 03 '15 at 15:11
  • 1
    You can also create your own interner if you want to cache values. See http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Interners.html. – Benjy Kessler May 03 '15 at 15:11
  • By the way, `long` and `int` are not cached; that's just the way primitive values work. They are always stored as-is in memory. However, the class file format does have a table of large constants. – Nayuki May 03 '15 at 15:12
  • 1
    Forget about Permanent Generation. It has gone in Java 8, and Java 7 is [no longer supported](https://www.java.com/en/download/faq/java_7.xml). All Java objects live in Heap (including cached Integers, Longs etc). – apangin May 03 '15 at 19:19

1 Answers1

0

Strings are special - they're the only non-primitive type that has syntax-level support for literals. As your quote says, String literals specifically (and not String instances constructed any other way) are always reused across the JVM. All other classes, including Long and Integer, only cache what they're designed to cache.

Look at the documentation for Integer.valueOf(int):

this method is likely to yield significantly better space and time performance [than new Integer()] by caching frequently requested values. This method will always cache values in the range -128 to 127, inclusive, and may cache other values outside of this range.

As you can see Integer supports caching, but only in certain cases. If you call new Integer() instead you will always get a new instance, even if another equivalent object exists. This is also true of String - if you call new String() the result is guaranteed to be a different object. In other words:

"foo" == new String("foo")

will always be false, regardless of Java's String literal caching behavior.

dimo414
  • 47,227
  • 18
  • 148
  • 244