Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

The string type is immutable.

We can use the const keyword with strings in high level language like .NET. My understanding of 'const' means constant (it remains the same, we can't change the value).

Are strings not always constant (IMO the term constant should not be applicable in the same context if the type has to be recreated each time it means for the values life time, it was constant)?

In high level languages, specifically .NET (although I'd be interested in Java too), is this due to general memory management/tracking of objects or is there another reason?

share|improve this question
6  
Which programming language do you use? The answer may depend on specific details of the language implementation. –  Viliam Búr Mar 15 '13 at 14:22
    
Your definition of "constant" is hard to understand. Do you mean "once created it always exists"? If yes, the answer should be obvious: you'd run out of memory. –  parsifal Mar 15 '13 at 14:24
1  
If that's not what you mean, then you're confusing objects and the variables that point to them. A "constant" in Java (and I assume .Net also) is simply a variable that can never be changed. –  parsifal Mar 15 '13 at 14:25
1  
@DaveRook In Java, the final modifier on a string variable prevents the variable to be changed to point to a different string. –  MichaelT Mar 15 '13 at 14:30
1  
What exactly needs to be constant, what exactly can't be modified: the characters in a specific string object, or an object in a specific variable? That's two different things. In Java, strings are immutable, which means that when you create a string object, you cannot modify it... but you can throw it away and put another object into the same variable. The constant string ("static final String" in Java) means that you cannot even replace the object in the variable. –  Viliam Búr Mar 15 '13 at 14:37

5 Answers 5

up vote 15 down vote accepted

You are confusing two different things:

  • Immutable means the object's memory contents cannot be modified. When you modify a string instead of overriding the contents in memory it instead allocates a new block of memory and then assigns the old pointer to the new, changed content.

  • Constant means the variable cannot be modified at compile-time. Whether a string or an integer the contents of the variable (or what it points to) cannot be changed or assigned at compile time.

share|improve this answer
    
Thank you. I am being far to literal but this answers it very well. –  Dave Mar 15 '13 at 14:39
1  
If you can't modify the constant at compile-time, when do you modify it? –  parsifal Mar 15 '13 at 15:36
1  
@parsifal If it is a const you cannot modify it. You can define it to an initial value, but you cannot assign a different value to it anywhere else, thus you cannot modify it. –  rae1 Mar 15 '13 at 16:12
    
@rae1n: this is not 100% correct. Constant means a variable cannot be modified run-time. It is a property of the variable which holds a string, while immutability is a property of the object itself. –  Doc Brown Mar 15 '13 at 16:27
    
@DocBrown Are you saying you can modify a Constant at compile-time? –  rae1 Mar 15 '13 at 16:34

As the comments say, you're confusing "constant" with "immutable", when they really mean two very different things.

A constant is a variable that can't be changed. Depending on the language and the compiler, it's entirely possible for compiled code to simply replace every use of that variable with the value assigned to it, in which case it would never even be allocated a spot in memory. Even if the compiler doesn't optimize that way, it's still allocated one spot in memory at launch (since const implies static) and stays there until exit.

An immutable is a value in memory that can't be changed (until the memory is reclaimed). It's assigned a place in memory as needed, and it can (theoretically) be stored in multiple variables. Assigning to a variable which is pointing at an immutable value simply creates a new immutable and alters the variable to point to that instead. The old value stays in memory until garbage collected, which can happen even while the program is running.

share|improve this answer
3  
+1 for explaining what constant is actually doing: inlining your variable's value everywhere it's referenced; which is an important mechanism to understand as it has even larger effects than just what's mentioned here. –  Jimmy Hoffa Mar 15 '13 at 14:42

Strings can't change. Pointers to strings can change. The following is valid code:

String text = "Text";
text = text.substring(1);

The original "Text" object is still there, unchanged and immutable, except nothing is pointing to it anymore. A mutable String would allow you to do text.substring(1) without having to assign it back to the text variable, and it would change the original object. If you want the variable text not to change, that's when you make it a const. There are valid use cases for both.

As a side note, this is a good example of a concept that's easier to understand in C++, because objects and pointers to objects have different types.

share|improve this answer

I believe that you're confusing variables and objects.

A variable is simply a reference to an object.

While your program is running, a variable can refer to multiple objects. Assigning something to the variable does not change the object. If there are no variables that reference an object, it becomes garbage.

A String is considered immutable (in Java, and I would also expect in .Net) because there is no way to change the object.

A constant is simply a variable that is not permitted to change during the life of the program. You define a constant with the final modifier in Java, and the const modifier in .Net.

However, if your "constant" variable points to a mutable object, the "constness" of the variable does not affect the mutability of the object (actually, I can't say that for sure in .Net, because I'm assuming it inherits some of the arcane rules of C++).

share|improve this answer
    
@Downvoter - would you care to comment? Or should I assume that you too do not know the difference between a variable and an object? –  parsifal Mar 15 '13 at 15:32
1  
I am not the downvoter - but do you consider that not all the world is Java or C#? –  Ingo Mar 16 '13 at 0:46
    
@Igno - well sure, except the OP said "specifically .NET (although I'd be interested in Java" so I limited my comments to those two. That said, the separation between variable and object also applies to JavaScript, Ruby, Python, Perl, and PHP. In fact, other than C++, I can't think of another modern language that claims to be object-oriented that doesn't make this distinction. –  parsifal Mar 16 '13 at 19:09

A simple demonstration of what const accompishes. This is applicable to C# and similar languages.

string noconst = "foo";
noconst = "bar"; //noconst has changed! It was declared as "foo" but now is "bar"

const string yesconst = "foo";
yesconst = "bar"; //compiler error
share|improve this answer
1  
While this demonstrates how const behaves, the behavior is not unique to const. In Java, the final keyword behaves similarly and in C# the readonly keyword does the same. The unique thing about a const is that it's known at compile-time. –  Corbin March Mar 15 '13 at 14:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.