"Why" + " is" + " " + "this" + " " + "ba" + "d?"



  • <FONT face=Tahoma>ok, i have been programming for a while now (about a year in the *real* world). and i haven't had much knowledge about the stuff to keep your code out of this site. :p

    that's why when i got to know TDWTF, i instantly became an avid fan. what's better than defeating the enemy than knowing your enemy right? although i already knew some wtf practices to be avoided i am still guilty of committing some wtf in my code but most of them is because i tried to do something by doing a different thing each time, but that was before.

    it's only when i really worked in the *real* world that i got exposed to best practices, code optimizations, abstractions and other stuff.

    now one day, i read something about not concatenating a large number of strings but instead suggests on using a StringBuilder class. i ask myself why is this needed? i further researched and learned that the string is immutable, which means a new string object is instantiated whenever the content changes.

    this doesn't really answer my question but adds another one...why the need for immutable objects?

    now maybe i'm looking really stupid here, but i'm just looking for justifications why the string is immutable and why StringBuilder is created to overcome this immutability? why not just create a mutable string in the first place?

    i tried looking for answers but all i got are similar definitions but with no real examples. or maybe i was not looking hard enough...

    anyway, hope you guys can help me.</FONT>



  • This should answer you: http://www.churchillobjects.com/c/11027b.html



    Basically it seems that because as in Java everything is a reference, mutable strings would be a nightmare with containers.



  • <FONT face=Tahoma>guess i'm not just looking harder... or maybe focusing too much on .net's explanation

    anyway thanks for the link, dude. :)</FONT>



  • @xrT said:

    <FONT face=Tahoma>ok, i have been programming for a while now (about a year in the real world). and i haven't had much knowledge about the stuff to keep your code out of this site. :p

    that's why when i got to know TDWTF, i instantly became an avid fan. what's better than defeating the enemy than knowing your enemy right? although i already knew some wtf practices to be avoided i am still guilty of committing some wtf in my code but most of them is because i tried to do something by doing a different thing each time, but that was before.

    it's only when i really worked in the real world that i got exposed to best practices, code optimizations, abstractions and other stuff.

    now one day, i read something about not concatenating a large number of strings but instead suggests on using a StringBuilder class. i ask myself why is this needed? i further researched and learned that the string is immutable, which means a new string object is instantiated whenever the content changes.

    this doesn't really answer my question but adds another one...why the need for immutable objects?

    now maybe i'm looking really stupid here, but i'm just looking for justifications why the string is immutable and why StringBuilder is created to overcome this immutability?

    • Most strings never need to be modified, modified strings are a small subset of all strings.
    • Immutable strings lead to easy optimizations such as pooling and interning
    • Immutable strings make hashing much simpler than immutable, as well as comparison when combined with pooling/interning
    • Immutable objects are inherently thread-safe and operations on immutable objects only are inherently side-effect free, this makes for a much safer environment (this is why functional language are extremely safe and highly parallelizable)

    The main drawback is that every string operation generates new strings. Quite a lot of language designers decided that this was acceptable and that other methods could be used to alleviate the issue.

    Oh, and in the case you have here mutable strings wouldn't change anything.

    And I'm 99% sure that the Java compiler often optimizes string concatenation to use Stringbuffers anyway.



  • @masklinn said:

    <FONT face=Tahoma>

    • Most strings never need to be modified, modified strings are a small subset of all strings.
    • Immutable strings lead to easy optimizations such as pooling and interning
    • Immutable strings make hashing much simpler than immutable, as well as comparison when combined with pooling/interning
    • Immutable objects are inherently thread-safe and operations on immutable objects only are inherently side-effect free, this makes for a much safer environment (this is why functional language are extremely safe and highly parallelizable)

    The main drawback is that every string operation generates new strings. Quite a lot of language designers decided that this was acceptable and that other methods could be used to alleviate the issue.

    Oh, and in the case you have here mutable strings wouldn't change anything.

    And I'm 99% sure that the Java compiler often optimizes string concatenation to use Stringbuffers anyway.




    hmm...more interesting topics to study, thanks!

    it's getting clearer now... :)

    </FONT>


  • becausespacesaregood



  • Java answer specific to the Sun and Eclipse compilers:


    It's not. A string expression evaluated once, no matter how long, is equivalent to appending all the strings to a string(buffer|builder) and then calling toString. It doesn't create a buffer/builder for each subexpression.


    The real problem arises when you have string concatenation in a loop, or if you break the long exression down into smaller expressions with named variables instead of anonymous temporaries:

    <font face="Courier New">String out = "";
    for (String s : someStrings)
      out += s + ","; // like so
    return out;
    </font>
    Doing this causes the compiler to generate code that goes back and forth between String and StringBuilder, creating and populating a new StringBuilder every time through the loop.  Yuck.



  • @tster said:

    becausespacesaregood


    <FONT size=2>

    </FONT><FONT size=3><FONT face=Tahoma>StringBuilder sb = </FONT><FONT face=Tahoma color=#000000>new</FONT></FONT><FONT face=Tahoma> StringBuilder();
    sb.AppendFormat("{0}{1}{2}{3}{4}", "n", "i", "c", "e", "!");
    sb.ToString();


    :)


    </FONT>


Log in to reply