aspose file tools*
The moose likes Java in General and the fly likes No. of String Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "No. of String Objects" Watch "No. of String Objects" New topic
Author

No. of String Objects

Shiva.Om Kumar
Ranch Hand

Joined: Jun 14, 2010
Posts: 68



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.
Rodmar Conde
Greenhorn

Joined: Jul 09, 2010
Posts: 11
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.

SCJA | SCJP Semi Study Guide in Spanish
Shiva.Om Kumar
Ranch Hand

Joined: Jun 14, 2010
Posts: 68
thanks for reply....
anyone else would like to share the info? please ...
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 440

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

SCJP 6 [86%] June 30th, 2010
OCPWCD [84%] March 26th, 2012
OCEJPAD [83%] September 6th, 2014
If you find any post useful, click the "plus one" sign on the right
Shiva.Om Kumar
Ranch Hand

Joined: Jun 14, 2010
Posts: 68
thanks for reply....
Garg Sharad
Greenhorn

Joined: Jun 20, 2009
Posts: 15
The answer would be 10.

Cheers,

Sharad Garg| SCJP6 95%
karthik varadarajulu
Greenhorn

Joined: Apr 24, 2010
Posts: 8
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 )
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 440

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
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18840
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18840
    
  40


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

Henry
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18840
    
  40

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.

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

Henry
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

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?


OCPJP 6
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18840
    
  40

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
Naresh Shanmugam
Ranch Hand

Joined: Jul 16, 2010
Posts: 84
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..

arun nidhin
Greenhorn

Joined: Jul 31, 2010
Posts: 1
Totally six string objects will be create.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
Shiva.Om Kumar wrote: . . . at line 2 . . .
It appears that by "2" you mean "4"
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
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?
Naresh Shanmugam
Ranch Hand

Joined: Jul 16, 2010
Posts: 84
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...

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
. . . but put the concatenation inside methods; the static fields don't produce any useful bytecode!
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

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?
Naresh Shanmugam
Ranch Hand

Joined: Jul 16, 2010
Posts: 84
Campbell, i inspected the byte code after compiling, now i am very clear.. Thanks for your valuable guidance
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
You're welcome How many String objects did you find? Was there any difference when you deleted "final"?
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

hey guyssss... how to inspect a byte code...is that a tool or something?? that might be useful..i guess..
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19695
    
  20

You can use javap for that.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ronald de Gier
Greenhorn

Joined: Sep 09, 2009
Posts: 9
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.
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

hey guyss..1 more quick question...

Will this create a String object (as "Hello" is a String literal)?
Ronald de Gier
Greenhorn

Joined: Sep 09, 2009
Posts: 9
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.
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

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?
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014
    
  10
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.
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

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.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014
    
  10
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.
Aalok Singh
Greenhorn

Joined: May 27, 2010
Posts: 14
Yes, Total no of objects created @ line 2 will be 5.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014
    
  10
No. Please read the thread above, particularly the posts by Henry. This has already been covered.
Aalok Singh
Greenhorn

Joined: May 27, 2010
Posts: 14
Thanks a lot Mike.


Rgds,

Singh
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19695
    
  20

Ajuba Singh wrote:Rgds,

Please UseRealWords.
 
Don't get me started about those stupid light bulbs.
 
subject: No. of String Objects