Results 1 to 11 of 11
  1. #1
    krishan is offline Member
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default help me in dis question

    Que:hw many string object will be formed when dis method is called
    public String f(){
    String s="fred";
    s=s+"47";
    s=s.substring(2,5);
    s=s.toUpperCase(); }
    Answer : 3
    i know String object r immutable but hw dis will work in dis programme

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default hhhmmm...

    Java Code:
    public String f()
    Well... it sounds like homework to me... but I can tell that according to the method definition, it has to be returning a string and I didn't see a return statement in the method.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

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

  4. #4
    krishan is offline Member
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default

    i really forgot to write return statement
    it is:
    return s.tostring();
    no plz help me finding answer how many string object will be created wen dis method is invoked

  5. #5
    ramsrocker is offline Member
    Join Date
    Feb 2009
    Posts
    48
    Rep Power
    0

    Default

    its only one string? one variable one string. your just editing the variable.

    if i get what your asking...it will return one string.

  6. #6
    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

    Quote Originally Posted by krishan View Post
    i really forgot to write return statement
    it is:
    return s.tostring();
    no plz help me finding answer how many string object will be created wen dis method is invoked
    As I ask earlier, how many you found there? Give a try lol, you are in the correct way. Strings are immutable, so.....

  7. #7
    krishan is offline Member
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default

    its not like dat evry timr u perform a string operation u get a new string object leaving old one uneffected

  8. #8
    krishan is offline Member
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default

    i found 4 string obj. but ans is 3
    1. orignal string
    2. +"47"
    3. substring()
    4. touppercase

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

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

  11. #11
    krishan is offline Member
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Now read bit about substring() and toUpperCase() methods. Well explained on Java doc.
    i have read about dese two function
    they both return new string leaving orignal unchanged
    so total objects are 4
    if i hv any wrong knowledge plz tell me.......

Similar Threads

  1. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 09:49 AM
  2. Hello everyone and my first question
    By htetnaing in forum New To Java
    Replies: 3
    Last Post: 01-26-2009, 04:49 PM
  3. Question
    By kenrocks in forum AWT / Swing
    Replies: 3
    Last Post: 12-09-2008, 06:07 AM
  4. Need help with a question please
    By sonal in forum New To Java
    Replies: 1
    Last Post: 11-29-2007, 10:17 PM
  5. question about rmi
    By leonard in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 05:19 AM

Posting Permissions

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