What is needed for String s = new String ("abc") when we can create String s = "abc"

According to my understanding

String s = new String("abc") will create two objects -

  • one in the string pool String (if "abc" is not already in the constant pool)
  • one in heap memory

Although there is more understanding of how many objects will actually be created and where.

In any case, I have the following questions -

  • Why is this not described in the java docs of the String class about how many objects will be created and where?
  • Why is new String(String) provided anyway in the String class if the strings are immutable ?. You can also assume that all strings created by either String s = "abc" or String s = new String("abc") will be available in the String constant pool?
  • String literals used when creating or adding to StringBuilder or StringBuffer are also included in the String constant pool or they remain in the heap memory.

Edit

The java docs for the intern() method clearly speaks of a constant pool, but says nothing in String(String) .

+5
source share
2 answers

Why is this not covered in the java docs of the String class about how many objects will be created and where?

This is described in the Docs of String.

The String class represents character strings. All string literals in Java programs, such as "abc," are implemented as instances of this class. Lines are constant; their values ​​cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable, they can be shared. For instance:

  String str = "abc"; 

is equivalent to:

  char data[] = {'a', 'b', 'c'}; String str = new String(data); 

And from the Java language specification

A String object has a constant (immutable) value.

String literals (§3.10.5) are references to instances of the String class.

And from JSL # 3.10.5. String literals

In addition, a string literal always refers to the same instance of the String class. This is because string literals, or, more generally, strings that are constant expression values ​​(§15.28), are “interned” to exchange unique instances using the String.intern method.

.

Why is a new String (String) provided anyway in the String class if the strings are immutable ?. You can also assume that all strings created by either string s = "abc" or String s = new String ("abc") will be available in the String constant pool?

Since String is an object, a valid way of declaring is

 String s = new String("abc"); 

But where is String s = "abc"; designed for other reasons

Java designers decided to keep the primitive types in an object-oriented language, instead of making everything an object in order to improve the performance of the language.

Since this is the most useful class. To ensure performance, Java String is designed to be between the primitive and the class.

String literals used when creating or adding to a StringBuilder or StringBuffer also go to the String constant pool or remain in the heap memory.

Let's look at an example.

 StringBuilder sb = new StringBuilder("abc"); 

The literal "abc" , available in the constant pool, and the sb object created on the heap.

Take a picture to read my old answer: How to initialize a string with ""?

+7
source

String s = new String ("abc") will create two objects:

  • one in the string pool String (if "abc" is not already in the constant pool)

No. He is in a constant pool. It was put there by the compiler.

  • one in heap memory

Right.

Although there is more understanding of how many objects will actually be created and where.

Many misunderstandings: only one correct understanding.

Why is this not covered in the java docs of the String class about how many objects will be created and where?

The sending of the question is incorrect. He is covered. It is not possible to accurately “how many lines have been created”, but since the question has been asked millions of times over the past 20 years, the answer is not entirely secret. Or should not be.

Why is a new String (String) provided anyway in the String class if the strings are immutable ?.

So you can create a new one.

You can also assume that all strings created by either String s = "abc" or String s = new String ("abc") will be available in the String constant pool?

No. Only string literals and the result of String.intern() are in the constant pool. And this is documented.

String literals used when creating or adding to a StringBuilder or StringBuffer also go to the String constant pool or remain in the heap memory.

All String literals are put into the constant pool by the compiler.

+1
source

Source: https://habr.com/ru/post/1233907/


All Articles