Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8400109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:28:47+00:00 2026-06-09T21:28:47+00:00

Some languages (C# or Java) have immutable strings while others (e.g. Ruby) have mutable

  • 0

Some languages (C# or Java) have immutable strings while others (e.g. Ruby) have mutable ones. What are the reasons behind those desgin choices?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:28:49+00:00Added an answer on June 9, 2026 at 9:28 pm

    One reason why immutable strings are good is that it makes Unicode support easier. Modern Unicode can no longer fit efficiently into a fixed-size data cell, which kills the one-to-one correspondence between string index and memory address which gives mutable strings their advantage.


    In the past, most Western applications used single-byte characters (various ASCII-based codings, or EBCDIC…), so you could usually handle them efficiently by treating strings as byte buffers (as in traditional C applications).

    When Unicode was fairly new, there wasn’t much requirement for anything outside the first 16 bits, so Java used double-byte characters for its Strings (and StringBuffers). This used twice the memory, and ignored any problems that might occur from Unicode extensions beyond 16 bits, but it was convenient at the time.

    Now Unicode is not so new, and while the most-used characters still fit in 16 bits, you can’t really get away with pretending the Basic Multilingual Plane is all that exists. If you want to honestly claim Unicode support, you need either variable-length characters or even larger (32-bit?) character cells.

    With variable-length characters, you can no longer index into an arbitrary-length string in O(1) time — barring additional information, you need to count from the beginning to figure out what the N’th character is. This also kills the main advantage of mutable string buffers: the ability to seamlessly modify substrings in place.

    Fortunately, most string manipulation doesn’t actually need this modify-in-place ability. Lexing, parsing, and search all proceed on a sequential, iterative basis, from beginning to end. General search-and-replace was never in-place to begin with, since the replacement string doesn’t have to be the same length as the original.


    Concatenating large numbers of substrings doesn’t actually need modify-in-place to be efficient, either. You do need to be more careful about it, though, since (as others have pointed out) a naive concatenation loop can easily be O(N^2) by allocating a new string for each of N partial substrings…

    One way to avoid naive concatenation is to provide a mutable StringBuffer or ConcatBuffer object designed to do concatenation efficiently. Another way would be to include an immutable string constructor that takes an iterator into a sequence of strings to be (efficiently) concatenated.

    But, more generally, it is possible to write an immutable string library that efficiently concatenates by reference. This kind of string is often called a “rope” or “cord” to suggest that it is at least a bit more heavyweight than the basic strings it’s composed of, but for concatenation purposes it is much more efficient, since it doesn’t need to recopy the data at all!

    The above Wikipedia link says that “rope” datastructures are O(log N) to concatenate, but the seminal paper “Purely Functional Data Structures” by Okasaki shows how to do concatenation in O(1) time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In PHP and some other scripting languages have the $var syntax while Java and
I've recent been reading about immutable strings Why can't strings be mutable in Java
I have a Web Service written in Java. I want to send some strings
Some static languages like Java seem to have very special rules for variables defined
I've been wondering why do some languages, such as Java run on Both Linux
After some experience with functional languages, I'm starting to use recursion more in Java
Compared with other languages, Java sometimes is pretty chatty and some of the core
In some languages where you cannot override the () operator, I have seen methods
I have heard of types being referred to as boxed in some languages. In
I had some experience on programming languages like Java, C#, Scala as well as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.