Results 1 to 17 of 17
  1. #1
    kevinsong is offline Member
    Join Date
    Jul 2008
    Posts
    7
    Rep Power
    0

    Default how many objects ?

    Hi everybody!

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

    how many objcts will be create when define variable str!!

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    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.

  3. #3
    venni2008 is offline Member
    Join Date
    Jul 2008
    Posts
    1
    Rep Power
    0

    Default

    This is stupid question i ever see in this sight.

  4. #4
    kevinsong is offline Member
    Join Date
    Jul 2008
    Posts
    7
    Rep Power
    0

    Default

    hi venni2008:

    you may can't answer if you don't know,but you should not say this is a stupid question.

  5. #5
    kevinsong is offline Member
    Join Date
    Jul 2008
    Posts
    7
    Rep Power
    0

    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

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    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.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,812
    Rep Power
    25

    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.

  8. #8
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Post 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:
    Java 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:
    Java 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

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,812
    Rep Power
    25

    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

  10. #10
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    See reply number 1.

    :sigh:

    How this thing could drag on so long. I'm sorry, but really.

  11. #11
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    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

  12. #12
    kevinsong is offline Member
    Join Date
    Jul 2008
    Posts
    7
    Rep Power
    0

    Default

    Result is 5

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    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.

  14. #14
    kevinsong is offline Member
    Join Date
    Jul 2008
    Posts
    7
    Rep Power
    0

    Default

    1:str
    2:"a"
    3:"b"
    4:"a"+"b"
    5:"c"

  15. #15
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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".

  16. #16
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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.

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,812
    Rep Power
    25

    Default

    I'm sure someone could write a java compiler that creates as many objects as kevinsong would like.

Similar Threads

  1. Objects and Classes
    By Aleve in forum New To Java
    Replies: 8
    Last Post: 12-31-2007, 09:05 AM
  2. how can objects themselves be copied???
    By ishakteyran in forum New To Java
    Replies: 1
    Last Post: 12-29-2007, 11:04 AM
  3. JSP implicit objects
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-26-2007, 11:12 AM
  4. Help with Objects!
    By Shorinhio in forum New To Java
    Replies: 1
    Last Post: 07-10-2007, 10:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •