The data type Integer is a value type, but a String is a reference type. Why?
A String is a reference type even though it has most of the characteristics of a value type such as being immutable and having == overloaded to compare the text rather than making sure they reference the same object.
Why a string is just a value type
A String is a reference type even though it has most of the characteristics of a value type such as being immutable and having == overloaded to compare the text rather than making sure they reference the same object.
Why a string is just a value type
Why isn't a string just a value type, then?
First of all let's understand what value types and reference types are and the difference between them in a glance.
The types can be classified depending on whether a variable of a specific type stores its own data or a pointer to the data. That means, instead of storing data, it stores its address. If it stores its own data it is a value type and if it holds a pointer to data elsewhere in memory it is a reference type.
First of all let's understand what value types and reference types are and the difference between them in a glance.
The types can be classified depending on whether a variable of a specific type stores its own data or a pointer to the data. That means, instead of storing data, it stores its address. If it stores its own data it is a value type and if it holds a pointer to data elsewhere in memory it is a reference type.
Value types are stored in the Stack whereas reference types are stored in the Heap.
Comments
Post a Comment