Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory Allocation to String Objects? [duplicate]

Since, In Java, Strings are implemented as references of type Object.Also, String constants are implemented as Objects.

For eg:

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

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

String str = "This is a String";

My question is What if i print multiple Strings using + operator in the same System.out statement like this,

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

Now, What I'm basically asking is that I want to know if "This is a String" and "This is another String" will take up different memory spaces or Same memory space??

Also, does addition of a variable between the Strings in println() statement make a difference?

For eg:

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

Other questions related to strings on this site don't answer my question specifically.Please put a light on this question ^_^

Any help would be appreciated

like image 478
Adil Avatar asked Feb 08 '23 09:02

Adil


2 Answers

What I'm basically asking is that I want to know if "This is a String" and "This is another String" will take up different memory spaces or Same memory space??

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

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

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

From the Java Documentation: String

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

like image 79
YoungHobbit Avatar answered Feb 14 '23 00:02

YoungHobbit


Since, In Java, Strings are implemented as references of type Object.Also, String constants are implemented as Objects.

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

With respect to "This is a String" + "This is another String", I am not sure whether the Java Language Specification mandates that compilers must handle this in a specific way, but you can safely expect any halfway decent compiler to evaluate this during compilation time, which means that it would most probably be equivalent to "This is a StringThis is another String".

The addition of a variable between the string constants does of course make a difference, because now the expression cannot be evaluated during compilation time, so two separate string objects will have to be generated by the compiler, one for "This is a String" and another for "This is another String". (Unless, of course, i happens to be a constant, in which case it can still be evaluated during compilation time.)

But I would suggest that all these are trifling technicalities, with negligible impact on performance, so even though it is a good thing to want to know how they work, it is not a good thing to allow this knowledge to force you to program differently than what would be most natural for you to code and to understand your code.

like image 39
Mike Nakis Avatar answered Feb 13 '23 23:02

Mike Nakis