Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

String s = new String (& quot;xyz") creates several objects

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "String s = new String (" xyz ") created several objects, interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "String s = new String (" xyz ") created a few objects"!

Starting 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 start with the evolution of the method area.

Before the jdk1.7 version, the constant pool existed in the method area, which is a logical part of the heap, which has a name called non-heap.

Version 1.7 puts the string constant pool on the heap.

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

Why should the method area change, just so that the following content of the article will not disagree because of the JDK version, and the next content will be discussed on the basis of the jdk1.8 version.

String s = new String ("xyz")

Let's start with a piece of code.

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

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

As a result, the ldc command creates a "xyz" object in the constant pool, then pushes it to the top of the Operand stack, then astore saves it to a local variable, and return returns.

Then take a 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, while ldc creates a "xyz" string object in the constant pool, and then invokespecial executes the constructor, astore_1 assignment, and return returns.

From the above two examples, we can see that String s = new String ("xyz"); two objects are created, while the three objects mentioned in some answers count the reference s as an object.

Another answer is that if xyz exists, two will be created, and if it does not exist, 3 will be created (including 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 notice that the # 2 ldc # after the two ldc represents the index, indicating that the second new String ("xyz") did not recreate the xyz object.

Some common instruction mnemonics mean:

Nop, do nothing.

Aconst_null, push the null to the top of the stack.

Iconst_i (variable number) to push int type I to the top of the stack. In the same way, you should know what it means to have lconst_0,fconst_0.

Ldc, pushing int,float or String constant values from the constant pool to the top of the stack.

Iload, which pushes the specified int type local variable to the top of the stack.

Istore, stores the int value at the top of the stack in the specified local variable. In the same way, astore_i stands for storing the referenced value at the top of the stack into the first local variable.

Dup, copy the top value of the stack and press the copy value into the top of the stack.

Invokevirtual, call the instance method.

Invokespecial, call superclass constructor method, instance initialization method, private method.

Invokestatic, calling the static method.

Invokeinterface, call the interface method.

Invokedynamic, which calls the dynamic link method.

New, create an object and press its reference value onto the top of the stack.

Summary

How many objects have been created?

If the xyz does not exist and the reference is an object, it is three

If the xyz does not exist and the reference is not an object, it is 2

If xyz exists and references count as objects, that's two.

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

At this point, I believe you have a deeper understanding of "String s = new String (" xyz ") created several objects. You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report