Java Basics (2) ------ constant pool

Category constant pool

  • Static constant pool : the * .class file constant pool, class file constant pool not only contains the string (digital) literal, also contains the class, method of information, most of the space occupied by the file class. This constant pool constants for storage of two categories: literals (e.g. defined as final variable text characters, etc.) , a symbol reference quantity , a reference symbol conceptual aspects belongs compiler principles, including the following three types of constant:
  1. The fully qualified name of the class and interface
  2. Field name and descriptor
  3. The method name and descriptor
  • Runtime constant pool : runtime constant pool file relative to the constant pool CLass Another important feature with dynamic, Java language does not require constant necessarily only compile to produce, that is not preset into Class file constant pool content method to enter the zone runtime constant pool, during operation may be new constant into the pool. The String intern () method looks at whether the strings are equal there is an equal constant pool, if there are references to the string is returned. If not, add your own string into the constant pool.

String constant pool example

  1. Direct use double quotes statement out of the String object will be stored directly in the constant pool.

  1. Create a String object using the new keyword will not be stored in the constant pool.

  1. String object if the statement is not enclosed in double quotes, you can use methods of the String intern provided. The String.intern () method is a Native, its role is: if the runtime constant pool already contains a string such as a String object to this content, it returns a reference constant pool of the string; if not, then at the constant created in the pool the same contents as this string string and returns a string constant pool reference created.

Comparison of examples related to the intern:

important point:

  • == comparison is memory address, equals the value of comparison
  • String concatenation when: + variables and variables directly with strings + string concatenation is not the same, the former creates a new object.
  1. When we should try to avoid using the string of multiple string concatenation, as this would re-create the object. If you need to change the string, you can use StringBuilder or StringBuffer.

Other constant pool

  • Most Java primitive wrapper classes have achieved a constant pool of technology, namely Byte, Short, Integer, Long, Character, Boolean; these five wrapper class by default create value [-128,127] of the corresponding type of cache data but beyond this range will still go to create a new object.

  • Two kinds of floating-point type of wrapper class Float, Double and did not realize the constant pool technology.

The benefits of using constant pool

To avoid the frequent create and destroy objects affect system performance to achieve a shared object, but also can save storage space, the same object occupies the same memory.

Guess you like

Origin juejin.im/post/5cfdf93e6fb9a07ee85c1e6f