This article is in Java Server, which contains my entire Series of Java articles for study or interview

(1) Introduction

What’s the difference between String STR and new String()? I was asked this question once before in the interview, and I wrote my own understanding of this topic after the interview. However, when I was reading Effect Java recently, I found that the use of String was also mentioned in the book, so I happened to talk about it together.

(2) About String

String is probably one of the most commonly used objects in Java. It’s not one of the eight basic data types, but as I peruse the project code, more than 80% of variables are defined in String.

public final class String
    implements java.io.Serializable.Comparable<String>, CharSequence {
    /** The value is used for character storage. */
    private final char value[];

    /** Cache the hash code for the string */
    private int hash; // Default to 0
}
Copy the code

The basic information about String can be seen in the source definition of String class:

1. In JDK8, the value of a String instance is stored in char data. (in JDK9, the storage of a String instance is changed from a char to a byte array because using a byte array can reduce the memory in half)

2. The String class is final, so String cannot be inherited. The value variable is final, so String instances cannot be modified

3. The String class implements Serializable, Comparable, and CharSequence interfaces.

(c) About the string constant pool

String constant pools are the stuff of virtual machines, but the next few questions need to be addressed. To make String strings reusable, something called String constant pooling is set up in the Java virtual machine.

Java designed a String constant pool to avoid generating large numbers of strings. Working principle, create a string, the JVM first to check whether there is value in the string constant pool the same string, if any, are no longer create, returns the string reference address directly, if not, create, and then into the string constants in the pool, and returns the newly created string reference address. So look at this code:

String s1="abc";
String s2="abc";
System.out.println(s1==s2);  / / return true
Copy the code

The result is true, since it refers to the same object.

The location of the string constant pool varies from JDK to JDK:

Prior to JDK1.7, the string constant pool was part of the runtime constant pool and was stored in the method area

After JDK1.7, the string constant pool was taken from the method area to the heap

What is the difference between String STR and new String()

Back to the beginning, what’s the difference between String STR and new String()? The difference is that strings created by String STR are stored in the String constant pool and can be reused. The String created by new String() is generated in the heap in the most standard object creation way, and a new String creates an object in the heap. The following diagram should illustrate the process.

S1 ==s2 returns true, s1==s3 returns false.

The new String() method creates several objects

Another good question, this one has one or two answers.

Write a piece of code:

public class test {
    public static void main(String[] args) {
        String s1=new String("ab"); }}Copy the code

Javap -v test.class

Create ab object in constant pool at position #3

2. Create ab object in heap

This creates two objects, one in the constant pool and one in the heap.

Write another piece of code:

public class test {
    public static void main(String[] args) {
        String s1="ab";
        String s2=new String("ab"); }}Copy the code

View bytecode in the same way:

S1 =”ab” creates ab in the constant pool at position #2. At this point, new String creates only one String in the heap, which is already in the constant pool.

In this case, only one object is created.

(6) Summary

Although you can write code any way you want, sometimes you can save a lot of resources by writing it differently. Such as:

String str=new String("abc")
Copy the code

Written as:

String str="abc"
Copy the code

It’s a small change, but it’s possible to avoid creating a million String instances in a frequently called method.

For programmers with some development experience, we recommend reading “Effect-Java,” which has many better ways to write code.

I’m fish boy. See you next time!