|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

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

07-11-2008, 10:05 PM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 551
|
|
|
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-12-2008, 12:16 AM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 1
|
|
|
This is stupid question i ever see in this sight.
|
|

07-12-2008, 10:42 AM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 7
|
|
|
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, 10:50 AM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 7
|
|
|
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, 11:02 AM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 551
|
|
|
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, 06:30 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
|
|
|
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, 07:54 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Southwest
Posts: 880
|
|
|
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:
// 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:
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.
__________________
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. .
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
|
|

07-12-2008, 10:24 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
|
|
|
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, 02:22 AM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 551
|
|
|
See reply number 1.
:sigh:
How this thing could drag on so long. I'm sorry, but really.
|
|

07-13-2008, 06:54 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Posts: 533
|
|
|
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 06:54 AM.
Reason: interview -> interviewer
|
|

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

07-16-2008, 05:10 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 5,075
|
|
|
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Someone helped you? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post. Help: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Resources: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Web: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Tips: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

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

07-16-2008, 05:17 PM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 551
|
|
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, 05:20 PM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 551
|
|
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, 07:59 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
|
|
|
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
|
|
|
|
|