After 10 years of work, how many objects did String s = new String("xyz") create?

I believe that every student who learns java is familiar with this question. As a classic interview question, I really think it’s quite a fucking question after working for so many years. You can still see many people discussing this question on the Internet. Among them, many people who have worked for many years have disputes. I think it is necessary to talk about this issue.

Speaking from the method area

The constant pool exists in the method area, and the method area has changed a lot before and after the jdk1.7 version, so let's talk about the evolution of the method area first.

Before the jdk1.7 version, the constant pool existed in the method area. The method area was a logical part of the heap . It had a name called non-heap .

Version 1.7 put the string constant pool on the heap.

After 1.8, the permanent generation is removed, the concept of method area is retained, the implementation of method area is changed to metaspace, and the constant pool is still in the heap.

Why say that the method area is changed, just because the following content of the article will not be divergent due to the version of JDK, the following content will be discussed based on the jdk1.8 version.

String s = new String(“xyz”);

First come a piece of code

public class Test {
    
    
    public static void main(String[] args) {
    
    
        String s = "xyz";
    }
}

Then we compile the code with javac, then use javap to decompile, execute javap -c Test

From the results, the ldc command creates an "xyz" object in the constant pool, and then pushes it to the top of the operand stack , then astore saves it to a local variable, and return returns.

Then look at the code in the second interview question

public class Test {
    
    
    public static void main(String[] args) {
    
    
        String s = new String("xyz");
    }
}

Same decompilation analysis

Obviously, we see that new creates a String object, and ldc creates an "xyz" string object in the constant pool. Then invokespecial executes the constructor, assigns astore_1, and returns.

Through the above two examples, we can know that String s = new String("xyz"); creates 2 objects, and some answers say that 3 objects are referred to as one object.

There are also answers saying that if xyz exists, 2 are created, and 3 if it does not exist (including the reference s), let's test it again.

public class Test {
    
    
    public static void main(String[] args) {
    
    
        String s = "xyz";
        String s2 = new String("xyz");
    }
}

From here, it is obvious that this is a combination of our examples 1 and 2, but pay attention to the **#2** after the two ldc, # represents the index, indicating that the second new String ("xyz") The xyz object is not recreated.

The meaning of some common instruction mnemonics:

  1. nop, do nothing.
  2. aconst_null, push null to the top of the stack.
  3. iconst_i (variable number), push int type i to the top of the stack. Similarly, there are lconst_0 and fconst_0, you should know what it means
  4. ldc, pushes constant values ​​of int, float or String type from the constant pool to the top of the stack.
  5. iload, push the specified int type local variable to the top of the stack.
  6. istore, store the int type value on the top of the stack into the specified local variable. Similarly, astore_i represents storing the top-referenced value of the stack into the i-th local variable.
  7. dup, copy the top value of the stack and push the copied value onto the top of the stack.
  8. invokevirtual, call the instance method.
  9. invokespecial, call the super class construction method, instance initialization method, private method.
  10. invokestatic, call a static method.
  11. invokeinterface, call the interface method.
  12. invokedynamic, call the dynamic link method.
  13. new, creates an object, and pushes its reference value onto the top of the stack.

to sum up

How many objects are created?

  1. If xyz does not exist and the reference counts as an object, then there are 3

  2. If xyz does not exist and the reference is not an object, then there are 2

  3. If xyz exists and the reference counts as an object, then there are 2

  4. If xyz exists and the reference is not an object, it is 1

Of course, I think that quotations are definitely not the target. The final answer should be 1 or 2. To be honest, this interview question should not appear in the primary interview question.

In addition, if you do not understand the decompiled bytecode instructions, follow the public account and reply to the keyword 111 to get the "Java Virtual Machine Specification" e-book.

Guess you like

Origin blog.csdn.net/awl910213/article/details/108701547