
07-11-2008, 05:31 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 7
Rep Power: 0
|
|
how many objects ?
Hi everybody!
Expr: String str = "a"+"b"+"c"
how many objcts will be create when define variable str!!
|
|

07-11-2008, 09:05 PM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 1,397
Rep Power: 3
|
|
|
Done like that, with all literal strings, one, since the will be optimized down to "abc" by the compiler. But I don't believe that that's how the question actually looked.
|
|

07-11-2008, 11:16 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 1
Rep Power: 0
|
|
|
This is stupid question i ever see in this sight.
|
|

07-12-2008, 09:42 AM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 7
Rep Power: 0
|
|
|
hi venni2008:
you may can't answer if you don't know,but you should not say this is a stupid question.
|
|

07-12-2008, 09:50 AM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 7
Rep Power: 0
|
|
|
sorry everybody!
qestion is how many objcts will be generate when compiled, it from interview,i just want to know result,please not say it is stupid question.
thanks
|
|

07-12-2008, 10:02 AM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 1,397
Rep Power: 3
|
|
|
Well, what's wrong with the first reply then?
Besides, you can't control what people write here, so begging people not to say something, or demanding that they simply not reply, simply makes you look like a whiner.
|
|

07-12-2008, 05:30 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
|
|
|
write a small test program with that statement it and use the javap command to show what code is generated. You should be able to find your answer in the byte code.
|
|

07-12-2008, 06:54 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Southwest
Posts: 1,018
Rep Power: 3
|
|
bye bye byte code
Originally Posted by Norm
|
|
write a small test program with that statement it and use the javap command to show what code is generated. You should be able to find your answer in the byte code.
|
If poster cannot see creation of temporary objects, then I'm sure I don't want to join the squabble:
|
Code:
|
// Six memory locations, plus instructions.
char[] I_care = {'I','_','c','a','r','e'};
String DoICare = new String(care);// |
Explain it to them. What the posters original sample does is documented in the String docs or I will find it for you. My code sample, though not explanatory nor exactly conformant to what the docs use as their explaination, is essentially what happens in either code sample. A method to avoid beyond prototyping - given the over-eager optimization here - is:
|
Code:
|
System.out.println("If I " + doCare + "d, I would" + new String(" Google"));
// Not that bad really. |
Bjarne Stroustrup's view of the matter is the definitive overview in this language group.
That is for your use. What should be extracted from that for you to share is the vast memory resources that are available compared to what is being asked here is so separated that the issue is all but vanishingly inconsequentail until we get to heavily loaded core loops in convluted code. Ususally but not always that yields to simple path analysis: Taking two Strings and concantenating them results in a compiler generated temporary string that is beyond the scope of the code shipped to the compiler.
You should bring your expertise from years in an actual code shop to the challenge of how to explain the premature optomization issue.
|
|

07-12-2008, 09:24 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
|
|
|
I wrote a simple program with the given statement and looked at it with javap. It looks like 1 object is created at compile time and none at runtime. The only code executed is copying the reference to the object created at compile time to the variable.
Bottom line answer: 1
|
|

07-13-2008, 01:22 AM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 1,397
Rep Power: 3
|
|
|
See reply number 1.
:sigh:
How this thing could drag on so long. I'm sorry, but really.
|
|

07-13-2008, 05:54 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Posts: 571
Rep Power: 2
|
|
|
Your interviewer is an idiot, you don't want to work there.
The answer depends on which compiler you are using, it matters little which language you are using.
nearly all compilers will see the constants and do "constant folding" at compile time, so that at run time, there is less to do. Any compiler better than that put out as a CS 401 compiler class project will do constant folding automatically.
Better compilers will recognize that you never use the resulting 'srt =" line, and optimize that away as well. So the answer can be "none" since the program is a silly no-op.
But it varies with details of the compiler.
Last edited by fishtoprecords; 07-13-2008 at 05:54 AM.
Reason: interview -> interviewer
|
|

07-16-2008, 04:04 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 7
Rep Power: 0
|
|
|
Result is 5
|
|

07-16-2008, 04:10 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,513
Rep Power: 11
|
|
|
I agreed with, there are five objects. And for me this is quite tricky question, but it's much better one I seen for a long time.
|
|

07-16-2008, 04:14 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 7
Rep Power: 0
|
|
|
1:str
2:"a"
3:"b"
4:"a"+"b"
5:"c"
|
|

07-16-2008, 04:17 PM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 1,397
Rep Power: 3
|
|
Originally Posted by kevinsong
|
|
Result is 5
|
Only if they are not (or at least not all) String literals. And, if you count the StringBuilders (or StringBuffers) that are temporarily used for the conatenations (when they are not all String literals), it is much more than that. But, as I said, if they are String literals (as shown in your post) it will be one, and one only, as the compiler will optimise it down to "abc".
|
|

07-16-2008, 04:20 PM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 1,397
Rep Power: 3
|
|
Originally Posted by kevinsong
|
1:str
2:"a"
3:"b"
4:"a"+"b"
5:"c"
|
Once again, only if they are not String literals. If they are (as you posted) you will have one, and only one.
|
|

07-16-2008, 06:59 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
|
|
|
I'm sure someone could write a java compiler that creates as many objects as kevinsong would like.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT +2. The time now is 03:20 PM.
|
|