This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Number Of Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Number Of Strings" Watch "Number Of Strings" New topic
Author

Number Of Strings

SomeswaraRao Vudattula
Greenhorn

Joined: Mar 28, 2005
Posts: 12
String str;
str = "one" + "two" + "three" + "four";


in the above two lines of code how many Strings will be created..?
what is the solution for minimising the number of Strings..?
Soni Prasad
Ranch Hand

Joined: Mar 09, 2005
Posts: 97
Only one String object "onetwothreefour" will be created.
To reduce creating string object do not use new operator as

String a = new String("abc");

rather do

String a = "abc";

soni.


SCJP 1.4, SCBCD 1.3
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
hi but will individual literals will not create string object in pool ?

just clear my doubt... here will 5 liteals will not be created in jvm...

are literals not part of string pool...

??


Thanks and Regards, Amit Taneja
Soni Prasad
Ranch Hand

Joined: Mar 09, 2005
Posts: 97
In
str = "one" + "two" + "three" + "four";

all the literals are constant string so they are not created in the pool but if you do like this

String str1 = "one";
String str2 = str1 + "two" + "three" + "four";//2

then there will be 5 string objects created in the pool because at line 2 str1 is not a constant string literal.

obj1 = "one"
obj2 = "two"
obj3 = "three"
obj4 = "four"
obj5 = "onetwothreefour"

all of them will be in pool.

soni.
Rohan Kayan
Ranch Hand

Joined: Sep 17, 2004
Posts: 123
str = "one" + "two" + "three" + "four";


In my view all these String object will be created in the pool of intern (String literal)

"one"
"two"
"three"
"four"
"onetwo"
"onetwothree"
"onetwothreefour"


I am not confirm be , send your comment on my view ......


SCWCD 1.4, SCJP 1.4
Jeff Hu
Greenhorn

Joined: Feb 03, 2005
Posts: 16
is the same as

Therefore, 7 String objects will be created, because unlike StringBuffer, we cannot change the content of String object (it's final!) once it has been created.

{"one", "two", "three", "four", "onetwo", "onetwothree", "onetwothreefour"}
our String object reference variable str refers to the last one. I think GC will take care of rest of them

[ May 11, 2005: Message edited by: Jeff Hu ]
[ May 11, 2005: Message edited by: Jeff Hu ]

SCJP 1.4, SCJA 1.0
Jon Egan
Ranch Hand

Joined: Mar 24, 2004
Posts: 83
I agree with Soni's description.

I believe the compiler will concatenate all these literals into one before the program even runs, as it can see that they are all "constant string literals" as Soni puts it.

In his other example, it's not "safe" for the compiler to concatenate the strings. Although I would guess, that "two", "three", and "four" would still get combined by the compiler, and you would end up with only 3 strings:

obj1 = "one" // referenced by str1
obj2 = "twothreefour" // in the string literal pool, but not referenced by the program code
obj3 = "onetwothreefour" // referenced by str2

Perhaps not, though, because of order of operations? Maybe you end up with 5 strings in Soni's example, but 3 strings if you do the following:



hrmmm.... the tough part of proving this programatically is that the whole design goal of Strings being immutable and constant Strings being put in a pool, is that your program should never know the difference... I remember seeing some posting where people used "==" tests, or maybe some kind of identity hashcode, to try to infer about the string literal pool...

This is just the sort of thing that is fully defined by the Java spec, so we can get a conclusive answer if someone who is not currently running late for work looks it up

-- Jon
Ravi Tanuku
Greenhorn

Joined: Apr 06, 2005
Posts: 9
Egan is absolutely correct. we can find out that by decompiling the class. The decompiled source contains the following.

"onetwothreefour" // for "one"+"two"+"three"+"four"
str1+"two"+"three" // for str1+"two"+"three"
"two"+"three"+str1 // for "twothree"+str1

That means the compiler joins the string literals where it can.
Jas Oberai
Ranch Hand

Joined: Jan 16, 2005
Posts: 231

"onetwothreefour" // for "one"+"two"+"three"+"four"
str1+"two"+"three" // for str1+"two"+"three"
"two"+"three"+str1 // for "twothree"+str1


Hi Ravi,
So does that means in the first case 1 objects,second case 3 objects and 3 case 2 objects would be created??

Thanks
p.s:I have a small doubt and i don't want to start a new thread for it.What if a questions has 3 correct answers(say a,b,c)..and i only give (a,b) as my answers.....would that be considered wrong...because in one of the mock tests i got fraction in my score..(eg75.5%).


SCJP 1.4 (88%)<br />SCWCD 1.4 (88%)
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
can anybody tell me
why the "string" litral will be called constant literal ?

i m still not clear about the string ...query...??

any any advance java person tell the correct answer..?

and

jas...i think in real exam if u give 2 rt answer instead of 3 ..it will be consider wrong...

the exam take the question as correct/incorrect for full answer ..as it will compare to database for correct answer...
Jeff Hu
Greenhorn

Joined: Feb 03, 2005
Posts: 16
Well....I don't agree.

First, let me make a correction
String s = "abc"; // create 1 String object, namely, "abc"
String s = new String("abc") // create 2 String objects, "abc" is in the pool, new String("abc") is in the normal memory, since String type is immutable.

If we use overloaded operator "+", what is type of its operands??? You have to supply overloaded method left and right operands which is String type as parameters.

Convince me if there is something wrong here
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
There will be one String object at runtime.
JLS 15.28 is your authoritative source (there is a lot of fallacy regarding this particular topic on this forum - I'm yet to determine why - a broken book perhaps?).
http://qa.jtiger.org/GetQAndA.action?qids=68


Tony Morris
Java Q&A (FAQ, Trivia)
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
This question was discussed many times here in ranch please refer to this thread .


Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Number Of Strings