This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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
"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%)
Joined: Mar 14, 2003
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..?
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...
Joined: Feb 03, 2005
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.
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