This discussion is archived
1 2 Previous Next 16 Replies Latest reply: Dec 24, 2008 6:03 PM by 807589 RSS

String object...

807589 Newbie
Currently Being Moderated
public class samp{
public static void main(String args[]){
          String str1=str();
System.out.println(str1);
}
     public static String str(){

String s = "dgsg";
s+="etre";
s=s.substring(1,3);
s = s.toUpperCase();
System.out.println(s);
return(s.toString());

}
}

I just ant to know how many objects are created.... And Explanations...
  • 1. Re: String object...
    jwenting Journeyer
    Currently Being Moderated
    we're not your homework service.
    Tell us what you think and why and someone MAY tell you whether you're right or not and why.
  • 2. Re: String object...
    807589 Newbie
    Currently Being Moderated
    This is SCJP Question.... The answer given is 3...Buit according to my idea it will be 5... Because sting is immutable... So for each and every modification it will create object..
  • 3. Re: String object...
    807589 Newbie
    Currently Being Moderated
    Muthukris wrote:
    This is SCJP Question....
    No, it isn't. You won't see a question like that on the SCJP.

    ~
  • 4. Re: String object...
    807589 Newbie
    Currently Being Moderated
    I think 3 used to be correct as neither substring nor toString created a new String. Created, since I think (but can't be bothered to look up) that the substring method has changed to create a new String. (It used to share a char array with its parent.)

    What escapes me is why you should care how many Strings are created.
  • 5. Re: String object...
    807589 Newbie
    Currently Being Moderated
    levi_h wrote:
    What escapes me is why you should care how many Strings are created.
    The SCJP cert is all about questions no one would otherwise care about.
  • 6. Re: String object...
    807589 Newbie
    Currently Being Moderated
    BigDaddyLoveHandles wrote:
    levi_h wrote:
    What escapes me is why you should care how many Strings are created.
    The SCJP cert is all about questions no one would otherwise care about.
    With the exception of questions like the one in the first post of this thread.

    ~
  • 7. Re: String object...
    baftos Expert
    Currently Being Moderated
    Muthukris wrote:
    This is SCJP Question.... The answer given is 3...Buit according to my idea it will be 5... Because sting is immutable... So for each and every modification it will create object..
    I am not sure if my methodology is correct, but I think it is and will give you the answer:
        int i = 1;
        String s = "dgsg";
        if ( s != s + "etre" ) i++;
        s+="etre";
        if ( s != s.substring(1,3)) i++;
        s=s.substring(1,3);
        if ( s != s.toUpperCase()) i++;
        s = s.toUpperCase();
        if ( s != s.toString()) i++;
        System.out.println(i);
    Did not know what to do about the System.out.println(s), but I believe no new string is created in there.
  • 8. Re: String object...
    807589 Newbie
    Currently Being Moderated
    baftos wrote:
    I am not sure if my methodology is correct, but I think it is and will give you the answer:
    No it won't. You're using != to compare the strings, so your results are meaningless.
  • 9. Re: String object...
    807589 Newbie
    Currently Being Moderated
    uncle_alice wrote:
    No it won't. You're using != to compare the strings, so your results are meaningless.
    No, he's using != to compare references, which I believe was intentional in this particular case (i.e., checking for the creation of a new object).

    ~
  • 10. Re: String object...
    baftos Expert
    Currently Being Moderated
    The use of != is extremly deliberate in this case.
  • 11. Re: String object...
    796440 Guru
    Currently Being Moderated
    levi_h wrote:
    I think 3 used to be correct as neither substring nor toString created a new String.
    substring has always had to create a new string when it's not returning the entire original string. toString never has.
  • 12. Re: String object...
    796440 Guru
    Currently Being Moderated
    Muthukris wrote:
    This is SCJP Question.... The answer given is 3...Buit according to my idea it will be 5... Because sting is immutable... So for each and every modification it will create object..
    Which 5 lines do you think create Strings?
  • 13. Re: String object...
    807589 Newbie
    Currently Being Moderated
    For this particular example, substring() and toUpperCase() will always create new strings, so the != test is valid. But both methods may return the original string if the new string would be identical to the original. Does the SCJP exam require you to explain why the code works the way it does with the given inputs, and what inputs would could yield different results? If not, it's bogus question.
  • 14. Re: String object...
    807589 Newbie
    Currently Being Moderated
    uncle_alice wrote:
    Does the SCJP exam require you to explain why the code works the way it does with the given inputs, and what inputs would could yield different results?
    Nope.
    If not, it's bogus question.
    Yep.

    ~
1 2 Previous Next