• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Number Of Strings

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..?
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...

??
 
Soni Prasad
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ......
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


"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%).
 
amit taneja
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question was discussed many times here in ranch please refer to this thread .
reply
    Bookmark Topic Watch Topic
  • New Topic