Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-11-2008, 05:31 PM
Member
 
Join Date: Jul 2008
Posts: 7
Rep Power: 0
kevinsong is on a distinguished road
Default how many objects ?
Hi everybody!

Expr: String str = "a"+"b"+"c"

how many objcts will be create when define variable str!!
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 07-11-2008, 09:05 PM
Senior Member
 
Join Date: Jun 2008
Posts: 1,397
Rep Power: 3
masijade is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-11-2008, 11:16 PM
Member
 
Join Date: Jul 2008
Posts: 1
Rep Power: 0
venni2008 is on a distinguished road
Default
This is stupid question i ever see in this sight.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-12-2008, 09:42 AM
Member
 
Join Date: Jul 2008
Posts: 7
Rep Power: 0
kevinsong is on a distinguished road
Default
hi venni2008:

you may can't answer if you don't know,but you should not say this is a stupid question.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-12-2008, 09:50 AM
Member
 
Join Date: Jul 2008
Posts: 7
Rep Power: 0
kevinsong is on a distinguished road
Default
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
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-12-2008, 10:02 AM
Senior Member
 
Join Date: Jun 2008
Posts: 1,397
Rep Power: 3
masijade is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 07-12-2008, 05:30 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
Norm is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 07-12-2008, 06:54 PM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 1,018
Rep Power: 3
Nicholas Jordan is on a distinguished road
Post bye bye byte code
Originally Posted by Norm View Post
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.
__________________
Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 07-12-2008, 09:24 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
Norm is on a distinguished road
Default
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
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 07-13-2008, 01:22 AM
Senior Member
 
Join Date: Jun 2008
Posts: 1,397
Rep Power: 3
masijade is on a distinguished road
Default
See reply number 1.

:sigh:

How this thing could drag on so long. I'm sorry, but really.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 07-13-2008, 05:54 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 571
Rep Power: 2
fishtoprecords is on a distinguished road
Default
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
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 07-16-2008, 04:04 PM
Member
 
Join Date: Jul 2008
Posts: 7
Rep Power: 0
kevinsong is on a distinguished road
Default
Result is 5
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 07-16-2008, 04:10 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,513
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
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.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 07-16-2008, 04:14 PM
Member
 
Join Date: Jul 2008
Posts: 7
Rep Power: 0
kevinsong is on a distinguished road
Default
1:str
2:"a"
3:"b"
4:"a"+"b"
5:"c"
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 07-16-2008, 04:17 PM
Senior Member
 
Join Date: Jun 2008
Posts: 1,397
Rep Power: 3
masijade is on a distinguished road
Default
Originally Posted by kevinsong View Post
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".
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 07-16-2008, 04:20 PM
Senior Member
 
Join Date: Jun 2008
Posts: 1,397
Rep Power: 3
masijade is on a distinguished road
Default
Originally Posted by kevinsong View Post
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.
Bookmark Post in Technorati
Reply With Quote
  #17 (permalink)  
Old 07-16-2008, 06:59 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
Norm is on a distinguished road
Default
I'm sure someone could write a java compiler that creates as many objects as kevinsong would like.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using the hibernate objects JavaForums Java Blogs 0 06-02-2008 03:21 PM
Objects and Classes Aleve New To Java 8 12-31-2007 09:05 AM
how can objects themselves be copied??? ishakteyran New To Java 1 12-29-2007 11:04 AM
JSP implicit objects Java Tip Java Tips 0 12-26-2007 11:12 AM
Help with Objects! Shorinhio New To Java 1 07-10-2007 10:32 PM


All times are GMT +2. The time now is 03:20 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org