Forums Register Login

No. of String Objects

+Pie Number of slices to send: Send



at line 2, how many string objects are created? I believe its 5 (x2, "abc" , "rst", "pqr", "123"); am I correct?
or it is just one String object is created i.e. x2.
+Pie Number of slices to send: Send
I don't remeber where do I read that during String initialization which involves concatenating what really happens (internally) is that a StringBuffer is created, then the append method is used to add each String and finally it's toString method is invoked.

According to that, the answer to your question would be: 1. Only one String object is created.
+Pie Number of slices to send: Send
thanks for reply....
anyone else would like to share the info? please ...
+Pie Number of slices to send: Send
yes
here only one String object will be created
as we know that
the String literals are constants
and the concatenation is done at the compile time and no new String objects are created at run time
so the total line 2 only creates a big String object and nothing more than that
+Pie Number of slices to send: Send
thanks for reply....
+Pie Number of slices to send: Send
The answer would be 10.
+Pie Number of slices to send: Send
1st:abc
2nd:abcx1
3rd:rst
4th:abcx1rst
5th:abcx1rstx1
6th:pqr
7th:abcx1rstx1pqr
8th:abcx1rstx1pqrx1
9th:123
10th:abcx1rstx1pqrx1123
(substitute the value of x1 in-place of x1, strings are immutable )
+Pie Number of slices to send: Send
well in the post above
I told just about the concatenation
the object created using concatenation will be one and the total number of objects including those String literals will be 5
+Pie Number of slices to send: Send
 

Shiva.Om Kumar wrote:


at line 2, how many string objects are created? I believe its 5 (x2, "abc" , "rst", "pqr", "123"); am I correct?
or it is just one String object is created i.e. x2.



The answer for line 2 is one string object. And the reason is... x1 is a compile time constant. It is a final string variable that is assigned to a constant expression at declaration. And since x1 is a compile time constant, then the expression at line 2 is a constant expression. So... one object, in the string pool, referenced by x2.

Now... what if x1 wasn't a compile time constant? Then the answer would be as stated -- 5 string object. 4 string objects, represents the 4 literals, in the string pool. And the resultant string object, which is calculated at runtime. There is also a stringbuilder object that is created to build this last string.

Henry
+Pie Number of slices to send: Send

BTW, this is way beyond the scope of the SCJP ... moving to java in general.

Henry
+Pie Number of slices to send: Send
 

Henry Wong wrote:

The answer for line 2 is one string object. And the reason is... x1 is a compile time constant. It is a final string variable that is assigned to a constant expression at declaration. And since x1 is a compile time constant, then the expression at line 2 is a constant expression. So... one object, in the string pool, referenced by x2.
Henry


Good explanation Henry, could you please explain, why the line 2 is constant expression.
+Pie Number of slices to send: Send
 

Abimaran Kugathasan wrote:
Good explanation Henry, could you please explain, why the line 2 is constant expression.



I wrote this a while back... which explains what is a constant expression.

https://coderanch.com/t/454384/java/java/compile-time-constant

Henry
+Pie Number of slices to send: Send
Henry, I understand the compile-time constants & the 2nd line becoming a constant-expression, but if x1 had not been final, still the second line would create a single String object, I guess. Am I wrong? Then what about java creating internal StringBuffers to append String literals when '+' operator is used? Anyway a single final String will be the result,right?
+Pie Number of slices to send: Send
 

Vinoth Kumar Kannan wrote:Henry, I understand the compile-time constants & the 2nd line becoming a constant-expression, but if x1 had not been final, still the second line would create a single String object, I guess. Am I wrong? Then what about java creating internal StringBuffers to append String literals when '+' operator is used? Anyway a single final String will be the result,right?



See previous post... specifically...

Now... what if x1 wasn't a compile time constant? Then the answer would be as stated -- 5 string object. 4 string objects, represents the 4 literals, in the string pool. And the resultant string object, which is calculated at runtime. There is also a stringbuilder object that is created to build this last string.



If x1 had not been final, then x1 would not be a compile time constant, and hence, as described above.

Henry
+Pie Number of slices to send: Send
Henry,

StringBuilder will be used internally if there are more than one string object to be concatenated in the same expression is it true??

Because in K&B i have learnt that

String a = "Hello"
String b = a + "Hi"

Will create three objects("Hello", "Hi", "Hello Hi"). In this case no String builder at all because concatenation is done only with 2 object.

So when there are more than one string to be concatenated like,

String a = "Hello"
String b = "Hi"

String c = a + b + "to" + "everyone" + "from" + "JavaRanch"

In this case "stringbuilder" will be created internally to perform multiple concatenation and result will converted to a suitable string object by invoking toString method of Stringbuilder..

+Pie Number of slices to send: Send
Totally six string objects will be create.

+Pie Number of slices to send: Send
 

Shiva.Om Kumar wrote: . . . at line 2 . . .

It appears that by "2" you mean "4"
+Pie Number of slices to send: Send
 

arun nidhin wrote:Totally six string objects will be create.

Welcome to the Ranch

The original question restricts the count to one line of the code. Have you seen Henry Wong's answer?
+Pie Number of slices to send: Send
My question is when will a StringBuilder be used internally for concatenation???

Will it be used when ever there is more than one concat at the same expression

String a = "Hello"
String b = "Hi"

String c = a + b + "to" + "everyone" + "from" + "JavaRanch"

as the above one or even with this,

String c = a + b

Under what circumstance a StringBuilder will be used... Any suggestions on this...

+Pie Number of slices to send: Send
At present, the multiple + concatenation operations are optimised with a StringBulder internally. So, a StringBuilder would normally be used. It is worthwhile compiling some code and inspecting the bytecode. That will answer your question.
+Pie Number of slices to send: Send
. . . but put the concatenation inside methods; the static fields don't produce any useful bytecode!
+Pie Number of slices to send: Send
 

Campbell Ritchie wrote:At present, the multiple + concatenation operations are optimised with a StringBulder internally. So, a StringBuilder would normally be used. It is worthwhile compiling some code and inspecting the bytecode. That will answer your question.


If so, mustn't

create only a single String Object?
+Pie Number of slices to send: Send
Campbell, i inspected the byte code after compiling, now i am very clear.. Thanks for your valuable guidance
+Pie Number of slices to send: Send
You're welcome How many String objects did you find? Was there any difference when you deleted "final"?
+Pie Number of slices to send: Send
hey guyssss... how to inspect a byte code...is that a tool or something?? that might be useful..i guess..
+Pie Number of slices to send: Send
You can use javap for that.
+Pie Number of slices to send: Send
But if the string is actually constructed at runtime (StringBuilder right?) aren't the string constants still loaded in the string constant pool for the StringBuilder to work with?

Edit: after decompilation I see that it is 1 constant String object. I don't see any StringBuilder at work at all, so this work is actually done during compilation? I thought
this because people said 'a StringBuilder is used to catenate the strings', which I thought meant runtime. But this is purely the compilation process, then?

Edit2: right, so StringBuilder is used to catenate string values at runtime when needed (not a constant expression), and catenation during compilation may or may not
use a StringBuilder but this is irrelevant - constant expressions result in 1 value. Think it's clear to me now.
+Pie Number of slices to send: Send
hey guyss..1 more quick question...

Will this create a String object (as "Hello" is a String literal)?
+Pie Number of slices to send: Send
Yes, one String object will be created and referenced from the String constant pool.

If more code uses the String literal "Hello": only one object is created and references to that object are used (there won't be > 1 String objects with "Hello" in the String constant pool).

System.out.println("Hello") ;
System.out.println("Hello") ;

--> one String object with "Hello" referenced from the String constant pool.
+Pie Number of slices to send: Send
Oh..goodness...I didnt know string literals inside system.out.println() too are first stored in the object heap and then printed!! Hey thanks, for this info!

The above is a part of my code. What I do is,I take up a huge data file,read all lines and extract some info from all lines(say characters from 5-10 in all lines)
When I run this, the first 30000+ lines reach up so fast..!!, but the printing eventually slows down gradually and almost takes 10-15 minutes for processing a 3.3 lakh line(record) file.
I guess, the reason it is initially fast is because initially '"processing Line..." + lineNumber' string objects are constructed faster and as number goes up, the JVM starts finding gaps to put other objects and slows down? Correct???
Or anyother possible explanation?
And 1 more question guys, with more than 3 lakh strings,would there be sufficient storage in string literal pool for storing the references?Will it ever get fully occupied/used up?
+Pie Number of slices to send: Send
 

Vinoth Kumar Kannan wrote:Oh..goodness...I didnt know string literals inside system.out.println() too are first stored in the object heap and then printed!!


There's nothing special about System.out.println() here. String literals anywhere in your code are first stored in the intern pool (which you call the literal pool), which is a special part of the object heap. They are stored there once when the class is loaded, and never again in the life of that JVM. Well, usually. Only one instance for that literal is necessary; all subsequent uses of that same literal just reference the original string, reusing it.

Vinoth Kumar Kannan wrote:What I do is,I take up a huge data file,read all lines and extract some info from all lines(say characters from 5-10 in all lines)
When I run this, the first 30000+ lines reach up so fast..!!, but the printing eventually slows down gradually and almost takes 10-15 minutes for processing a 3.3 lakh line(record) file.
I guess, the reason it is initially fast is because initially '"processing Line..." + lineNumber' string objects are constructed faster and as number goes up, the JVM starts finding gaps to put other objects and slows down? Correct???
Or anyother possible explanation?
And 1 more question guys, with more than 3 lakh strings,would there be sufficient storage in string literal pool for storing the references?Will it ever get fully occupied/used up?


In the code shown, you have one and only one String literal: "processing Line...". That's the only thing that gets put in the String intern pool. Nothing else in the code show would cause any increasing memory usage - especially not in the String intern pool. But the behavior you describe does suggest there may be a memory problem somewhere - perhaps in the code you haven't shown. (Particularly, the code inside the loop.)

My guess is that your actual problem here has little to do with the rest of this thread. I suggest posting a new, separate thread where you include more of the code inside the loop.
+Pie Number of slices to send: Send
 

Mike Simmons wrote:
In the code shown, you have one and only one String literal: "processing Line...". That's the only thing that gets put in the String intern pool. Nothing else in the code show would cause any increasing memory usage - especially not in the String intern pool.


Doesnt the JVM internally create a StringBuilder object everytime the loop runs and appends "Processing Line.." string literal with the lineNumber int and produce a new String & store that in the object heap before printing?

and regarding performance, sure i can pull up a new thread with more code details.
+Pie Number of slices to send: Send
 

Vinoth Kumar Kannan wrote:Doesnt the JVM internally create a StringBuilder object everytime the loop runs and appends "Processing Line.." string literal with the lineNumber int and produce a new String & store that in the object heap before printing?


Yes. Or at least, probably yes. But nothing in your last two posts has anything to do with the string intern pool. It's hard to tell what you are really asking about, here. Please, post in a new thread. Posting your questions here is confusing to people reading the old thread, and confusing to people trying to understand whatever you are trying to talk about. It's time for a fresh start. Not here. New thread. Go! Thank you.
+Pie Number of slices to send: Send
Yes, Total no of objects created @ line 2 will be 5.
+Pie Number of slices to send: Send
No. Please read the thread above, particularly the posts by Henry. This has already been covered.
+Pie Number of slices to send: Send
Thanks a lot Mike.


Rgds,

Singh
+Pie Number of slices to send: Send
 

Ajuba Singh wrote:Rgds,


Please UseRealWords.
and POOF! You're gone! But look, this tiny ad is still here:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 2066 times.
Similar Threads
Using == with Strings
how many eligible for Garbage Collection
About Strings...
String equals
A String Question
mock question about "case"
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 04:04:23.