Confusion about declaring String objects

If I declare String as

String test=new String("testing"); 

and

 String test1="testing1" 

Since String is a class in JAVA, like test1 is a String object without using a new operator. Also, when a new statement is used, memory is assigned to a new String ("testing"), so in the case of test1, how is the memory allocated? Also, when a string is interned, if two strings have the same value with which link is the String store once in the old String pool?

+4
source share
5 answers

Consider first String test=new String("testing");

  • It creates a String object in Heap.No Validation is performed in the String Pool for the existence of this String in the pool.

and now this is String test1="testing1"

  • It creates a String object String in a string pool not in Heap.Before. A Creation check is performed regardless of whether this row exists in the pool. If so, then its link is returned; otherwise, a new String is created in the pool, and its link is return.Basically it is String Literal, which is placed in the constant pool to optimize memory and reuse.

intern (): It is used when you create an object using new () and you call intern () on that object, then the check is first performed in the Stirng pool, if this line already exists or not, if so, then it is used directly

+2
source

Java has separate memory for strings that are created without calling the constructor with new . Each time such a line is created, Java checks to see if this line is in this memory. If so, then Java sets the same link to a new line until one of them changes.

When you create a String with a constructor using new , it behaves like a regular object in Java.

Take a look at this example:

 String s1 = "Test"; String s2 = "Test"; 

When you compare this string with the == operator, it will return true. s1.equals(s2) will also return true.

It looks like if you create String objects with the following constructor:

 String s1 = new String("Test"); String s2 = new String("Test"); 

When you now compare these strings with the == operator, it will return false, because the link to these strings is now different (you created 2 unique String objects). But if you use s1.equals(s2) , it will return true as expected.

+1
source

When you use

 String test1="testing1" 

this means that you save only one copy of each individual string value

but

 String test=new String("testing"); 

provides a new string object.

0
source

Consider your second assignment:

 String1 test1 = System.getenv("PATH"); 

Here test1 is most likely also a reference to a String object, without using new (). You can assign references to existing objects to new variables. So where is the problem?

The problem is that you should not use sloppy wording like "test1 - String object". Is not. This is a reference to a String or null object. It is all about that.

0
source

Each time you create a string literal, the JVM first checks the pool of constant strings. If the row already exists in the pool, a link to the merged instance is returned. If there is no row in the pool, a new instance of the row is created and placed in the pool. For instance:

String s1 = "Welcome,"
String s2 = "Welcome"; // will not create a new instance

In the above example, only one object will be created. First, the JVM will not find a string object with a value of "Welcome" in the string constant pool, so it will create a new object. After that, he will find the line with the value "Welcome" in the pool, it will not create a new object, but will return a link to the same instance.

more visit http://www.javatpoint.com/java-string ?

0
source

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


All Articles