CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Strings

  1. #1
    Join Date
    Apr 1999
    Location
    Houston,Tx
    Posts
    9

    Strings

    Q.1

    public class Quest{
    public static void main(String[] arg){
    String s1 ="abc";
    String s2 ="def";
    String s3 =s1.concat(s2.toUpperCase());
    system.out.println(s1+s2+s3);
    }
    }

    Ans. abcdefabcDEF - I got this answer and it contradicts with the concept that String Objects are immutable.
    How does it work?

    Thanks,
    Deepa




  2. #2
    Join Date
    Aug 1999
    Location
    A.P,India.
    Posts
    8

    Re: Strings

    Hai Deepa,

    String class is immutable. But StringBuffer class is mutable. String concatenation is immplemanted through
    StringBuffer class and its append method.
    Hope you understand


  3. #3
    Guest

    Re: Strings

    Deepa,

    Strings are immutable. You can see that s1 or s2 remains unchanged.
    The concat does not affect the string object on which it is called.
    Also the toUppercase does not affect the string object.
    These methods return a cha

  4. #4
    Guest

    Re: Strings

    Java language supports + operator which is same concat method in string.The + operator or concat method uses StringBuffer and append internally to change the String.So Strings are immutable is still true. The + operator/concat method is provided as this was available in C++,I guess.




Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured