0

Which is the difference between: "string test;". "string test = "" ; ". "string test = null; "

Maibe my doubt is idiot, but, O would like to know the reason of I use the null value in variables, mainly in numerics vars.

7th Oct 2018, 12:26 PM
Gabriel Felix dos Santos
Gabriel Felix dos Santos - avatar
6 Respuestas
+ 4
"string test;" and "string test = null;" are equivalent, because string is reference type and null is default value for these. (In any case, the compiler requires assigning the value to any local variable before using it, therefore, if you write just "string test;", and try to output the value of test to the console, the project will not compile) Reference types store not values, but references to values in the heap (you can find more about heap in the course or google). And null means that link doesn't exist. string test = ""; in this case, test refers to ""(empty string) value in the heap. But null is the absence of any value. P.S. you cannot use null value for base value-types like int, double, float, long...
10th Oct 2018, 11:47 PM
Daniel Holmes
Daniel Holmes - avatar
+ 3
Gabriel Felix dos Santos, Roughly speaking, there is an array of characters in the heap. And string variable stores a reference to this array. In fact, a more complex String object is stored on the heap. For example, see the simplified model: string name = "John"; in the heap: ['J', 'o', 'h', 'n']; and the name variable refers to this "array". string name = ""; in the heap: [] (empty "array"); with reference is similar string name = null; in this case there is nothing in the heap, name variable has no link P.S. In practice, the link points to an object of type String, which stores an array of characters.
11th Oct 2018, 12:23 PM
Daniel Holmes
Daniel Holmes - avatar
+ 2
NULL value can be used when you need indefinite value. You cannot perform operations with NULL value, such as addition or getting length from it.
11th Oct 2018, 12:08 AM
Daniel Holmes
Daniel Holmes - avatar
+ 1
Thank you so much ✌✌
11th Oct 2018, 11:19 AM
Gabriel Felix dos Santos
Gabriel Felix dos Santos - avatar
0
So, how string is a reference type, it means the string = "hello" has references of each char values in the heap?
11th Oct 2018, 11:21 AM
Gabriel Felix dos Santos
Gabriel Felix dos Santos - avatar
0
wow, I got it. Thank you so much
11th Oct 2018, 3:49 PM
Gabriel Felix dos Santos
Gabriel Felix dos Santos - avatar