Home > Java > javaTutorial > body text

Analyze the role of intern() in Java

Y2J
Release: 2017-05-19 09:43:43
Original
3016 people have browsed it

I noticed the intern() method of String while reading a book today. I have never used it before. I have only seen this method before, but I have not looked at it carefully. So I took a look today. Personally, I think adding this method to the String class may be to improve performance a little bit, because fetching data from the constant pool is faster than fetching data from the heap. (Personal feeling)

 APIThe few sentences about this method, in fact, the summary is that after calling this method, stringobject Join the constant pool. We all know that the constant pool exists in the method area. It is part of the method area, and the method area is shared by threads, so the constant pool is also shared by threads, but it is not thread-freeSafety is actually thread-safe. It just allows references with the same value to point to the same location. If the reference value changes, but there is no new value in the constant pool, Then a new constant result will be opened to hand over to the new reference, instead of the same object, new's string and the direct assignment to the variable like the thread is not synchronized. The strings are stored in different locations. The former is in the heap, while the latter is in the constant pool. In addition, when doing string splicing operations, that is, when strings are combined with "+", the result is that there is In the constant pool or heap, this may vary depending on the situation. I wrote a few lines of code to test it.

First, the results:

1. Assign a value when directly defining a string variable. If there is only a string constant on the right side of the expression, then the variable is stored in the constant pool. in.

2. The string produced by new is stored in the heap.

3. When concatenating strings, that is, when doing the "+" operation, there are two situations:

i. The right side of the expression is a pure string constant, then it is stored in Inside the stack.

  ii. If there is a string reference on the right side of the expression, which is the handle of the string object, it will be stored in the heap.

   String str1 = "aaa";
        String str2 = "bbb";
        String str3 = "aaabbb";
        String str4 = str1 + str2;
        String str5 = "aaa" + "bbb";
        System.out.
print
ln(str3 == str4); // false
        System.out.println(str3 == str4.intern()); // true
        System.out.println(str3 == str5);// true
Copy after login

Result: str1, str2, str3, and str5 all exist in the constant pool. Since str4 has a reference type on the right half of the expression, str4 exists in the heap memory, while str5 has no reference type on the right side of the expression. Pure string constants are stored in the constant pool. In fact, Integer -128 ~ +127 of this packaging type is also stored in the constant pool. For example, Integer i1 = 10; Integer i2 = 10; i1 == i2. The result is true, which is probably for Performance optimization.

【Related recommendations】

1. Java free video tutorial

2. Detailed explanation of intern() in String object

3. In-depth analysis of the intern() method in Java

4. Summary of experience in using the intern() method in JAVA

5. What is the concept of intern method in java

The above is the detailed content of Analyze the role of intern() in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!