[Basic Java] What happens if you declare String Class and change what you declare?

Asked 2 years ago, Updated 2 years ago, 51 views

When using strings in Java

String str1 = "hello"; We have learned that is declared in the constant domain.

But if you declare it in the constant field, String str1 = "hello"; String str2 = "hello"; If you point to the same hello like this, str1 and str2 point to the same position, and while doing this, I had a question.

If I write [str1 = "h";] under [String str1 = "hello";] str1 does not change hello to h in the existing position, but creates a new place called h in the constant region?

Thank you for your answer ^

^

java string

2022-09-22 13:17

1 Answers

Yes, that's right. Assign "h" to the constant region and point to it.

Also, existing hellos are subject to garbage collection if there is no reference (from Oracle jvm version 7).

In the old version (sun jvm 6 and below), the constant pool was not subject to garbage collection.

However, permgen was changed to heap area and was targeted for garbage collection.


2022-09-22 13:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.