Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





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.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-11-2008, 06:31 PM
Member
 
Join Date: Jul 2008
Posts: 7
kevinsong is on a distinguished road
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
Sponsored Links
  #2 (permalink)  
Old 07-11-2008, 10:05 PM
Senior Member
 
Join Date: Jun 2008
Posts: 551
masijade is on a distinguished road
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-12-2008, 12:16 AM
Member
 
Join Date: Jul 2008
Posts: 1
venni2008 is on a distinguished road
This is stupid question i ever see in this sight.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-12-2008, 10:42 AM
Member
 
Join Date: Jul 2008
Posts: 7
kevinsong is on a distinguished road
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, 10:50 AM
Member
 
Join Date: Jul 2008
Posts: 7
kevinsong is on a distinguished road
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, 11:02 AM
Senior Member
 
Join Date: Jun 2008
Posts: 551
masijade is on a distinguished road
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, 06:30 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
Norm is on a distinguished road
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, 07:54 PM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 880
Nicholas Jordan is on a distinguished road
bye bye byte code
Quote:
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.
__________________

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
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 07-12-2008, 10:24 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
Norm is on a distinguished road
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, 02:22 AM
Senior Member
 
Join Date: Jun 2008
Posts: 551
masijade is on a distinguished road
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, 06:54 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 533
fishtoprecords is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 07-16-2008, 05:04 PM
Member
 
Join Date: Jul 2008
Posts: 7
kevinsong is on a distinguished road
Result is 5
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 07-16-2008, 05:10 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 5,075
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 07-16-2008, 05:14 PM
Member
 
Join Date: Jul 2008
Posts: 7
kevinsong is on a distinguished road
1:str
2:"a"
3:"b"
4:"a"+"b"
5:"c"
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 07-16-2008, 05:17 PM
Senior Member
 
Join Date: Jun 2008
Posts: 551
masijade is on a distinguished road
Quote:
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, 05:20 PM
Senior Member
 
Join Date: Jun 2008
Posts: 551
masijade is on a distinguished road
Quote:
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, 07:59 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
Norm is on a distinguished road
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
Sponsored Links
Reply


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

vB 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 04:21 PM
Objects and Classes Aleve New To Java 8 12-31-2007 10:05 AM
how can objects themselves be copied??? ishakteyran New To Java 1 12-29-2007 12:04 PM
JSP implicit objects Java Tip Java Tips 0 12-26-2007 12:12 PM
Help with Objects! Shorinhio New To Java 1 07-10-2007 11:32 PM


All times are GMT +3. The time now is 08:20 AM.


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