JavaReferences and strings

Asked 2 years ago, Updated 2 years ago, 123 views

The reference variable that stores the class's address value is System.out.print(), which shows the address value at the time of output.

The reference variable referring to the string class is System.out.print(), and why is the actual value, not the address value, output at the time of output?

java reference-variable reference string string-literals

2022-09-21 20:54

1 Answers

If you simply write down the answer, it's because of the toString method.

If you override the toString method for the general class, you can print out the value at println.

But in Java, strings are a little bit unique.

To create a string object, see

1. String str = "abcd";
2. String str = new String ("abcd");

There are two methods, as shown in , and they are stored in memory in different ways.

Within jvm, there is an area called constant pool.

A string generated in literal format, such as format 1, is stored in the content pool, and if the value is the same, it refers to the same item. (See the internal method.)

When you create it like type 2, it is created in heap like a normal object and stores its address.

As a result, it is advantageous to create a string in the form 1.

It would be helpful to take a closer look at the permanent area and why the permanent area has changed to metaspace.


2022-09-21 20:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.