Allocating memory to string objects?

Since in Java strings are constructed as links of type Object . String constants implemented as objects.

For instance,

 System.out.println("This is a String"); 

In the above statement, "This is a String" is also implemented as an Object type. This means that a memory will be allocated that will be the same as

 String str = "This is a String"; 

My question is: if I print multiple lines using the + operator in the same System.out expression like this,

 System.out.println("This is a String" + "This is another String"); 

Now, I basically ask what I want to know if "This is a String" and "This is another String" occupied by different memory spaces or the same memory space ??

Is also a variable added between the lines in the println() expression?

For instance,

 int i = 10; System.out.println("This is a String" + i + "This is another String"); 

Other questions related to the lines on this site do not specifically answer my question. Please inquire about this question ^ _ ^

Any help would be appreciated

+5
source share
2 answers

Basically I ask if I want to know if "This is a String" and "This is another String" occupied by different memory spaces or the same memory space?

Here, and String are constants, so at compile time they will be merged, and a single String "This is a StringThis is another String" will be created in the string pool.

System.out.println ("This is the line" + I + "This is another line");

The actual String output is determined at run time, so it will create different String in the heap area.

From the Java documentation: String

The Java language provides special support for the string concatenation operator ( + ) and for converting other objects to strings. String concatenation is implemented through the StringBuilder (or StringBuffer ) class and its add method. String conversions are implemented using the toString method defined by Object and inherited by all Java classes.

+6
source

Since Java constructs strings as references of type Object.Also, string constants are implemented as objects.

No, in Java strings are implemented as string objects. These, of course, are objects. And internal objects are manipulated by reference. And string constants are again implemented as strings.

Regarding "This is a String" + "This is another String" , I'm not sure that the Java language specification requires compilers to handle this in a certain way, but you can safely expect some decent decent compiler to evaluate this at compile time, which means it will most likely be equivalent to "This is a StringThis is another String" .

Adding a variable between string constants, of course, matters because now the expression cannot be evaluated at compile time, so the compiler will have to generate two separate string objects, one for "This is a String" and the other for "This is another String" . (Unless, of course, i is a constant, in which case it can still be evaluated at compile time.)

But I would suggest that all this is nonsense, with little impact on performance, so even if it’s good to know how they work, it’s not good to let this knowledge make you program different from what would be most natural for you to encode and understand your code.

+2
source

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


All Articles