In C# if we want to create a variable of type string
we can use:
string str="samplestring"; // this will allocate the space to hold the string
In C#, string
is a class type, so if we want to create an object, normally we have to use the new
keyword. So how is allocation happening without new
or constructors
?
Strings are stored on the heap area in a separate memory location known as String Constant pool. String constant pool: It is a separate block of memory where all the String variables are held. String str1 = "Hello"; directly, then JVM creates a String object with the given value in a String constant pool.
Stack space contains specific values that are short-lived whereas Heap space used by Java Runtime to allocate memory to objects and JRE classes. In Java, strings are stored in the heap area. Why Java strings stored in Heap, not in Stack? String Literal is created by using a double quote.
Explanation: The object memory allocation takes place when the object constructor is called. Declaration of an object doesn't mean that memory is allocated for its members. If object is initialized with another object, it may just get a reference to the previously created object.
4. String Literal vs String Object. When we create a String object using the new() operator, it always creates a new object in heap memory.
When you write
string str="samplestring";
compiler will generate two instructions:
ldstr
gets a string literal from the metadata; allocates the requisite amount of memory; creates a new String
object and pushes the reference to it onto the stack.stloc
(or one of it's short forms, e.g. stloc.0
) stores that reference in the local variable str
.Note, that ldstr
will allocate memory only once for each sequence of characters.
So in example below both variables will point at the same object in memory:
// CLR will allocate memory and create a new String object
// from the string literal stored in the metadata
string a = "abc";
// CLR won't create a new String object. Instead, it will look up for an existing
// reference pointing to the String object created from "abc" literal
string b = "abc";
This process is known as string interning.
Also, as you know, in .NET strings are immutable. So the contents of a String
object cannot be changed after the object is created. That is, every time you're concatenating string, CLR will create a new String
object.
For example, the following lines of code:
string a = "abc";
string b = a + "xyz";
Will be compiled into the following IL (not exactly, of course):
ldstr
will allocate memory and create a new String
object from "abc"
literalstloc
will store the reference to that object in the local variable a
ldloc
will push that reference onto the stackldstr
will allocate memory and create a new String
object from "xyz"
literalcall
will invoke the System.String::Concat
on these String
objects on the stackSystem.String::Concat
will be decomposed into dozens of IL instructions and internal calls. Which, in short, will check lengths of both strings and allocate the requisite amount of memory to store the concatenation result and then copy those strings into the newly allocated memory.stloc
will store the reference to the newly created string in the local variable b
This is simply the C# compiler giving you a shortcut by allowing string literals.
If you'd rather, you can instantiate a string by any number of different constructors. For example:
char[] chars = { 'w', 'o', 'r', 'd' };
string myStr = new String(chars);
According to the MS documentation you do not need to use the new command to use the default string constructor.
However this does work.
char[] letters = { 'A', 'B', 'C' };
string alphabet = new string(letters);
c# Strings (from MSDN programming guide)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With