Forgot username/password? Don't have an account?
Login with Facebook
Home » Courses » Smooth Operator » Default Values
Subscribe for updates.
* New to us? log in with Facebook and get a free day pass

Default Values

Default Values

We know how to declare a variable and assign a value to it. Have you ever wondered what happens if we declare a variable and don't assign any value to it? In this case, the application has to reserve memory for the variable, irrespective of whether the value is assigned or not. The memory size depends upon the type of variable declared. This memory location will be occupied by the value we assign later. If we don't assign any value and proceed to the next command, the compiler saves a default value for that memory space. All such values assigned by the application are termed as default values.

Default values for different data types

Let's see how the application assigns values for different types of data. When we declare a variable of type string, for example var str:String; the default value for this variable is nothing -- or null. Since we didn't assign a value to str, no memory is assigned to it. Similarly with a Number, the default value that is assigned is NAN (not a number). But an int has a set of values: Z= {...-3.-2,-1, 0, 1, 2,....} and uint has all the positive integers {0, 1, 2, ...}. For these data types, the empty value or the default value is zero. Finally, for a Boolean data type, there are only two possible values: true or false. The value closest to nothing in this case is false. It's not exactly equal to null, but the nearest value to nothing among the two is definitely false. So, the application assigns false as the Boolean variable default.

Assign values to all the variables manually

When we trace these variables, we can find the above mentioned default values in the output panel. But when we depend on defaults (lazy behavior!) in big programs, this might cause confusion. So to avoid any difficulty, we must always assign a value to the variable soon after it's declared. Even if we want the variable to be empty, we should assign some value which is considered as nothing. For example, assign 0 to an int or uint, instead of asking the compiler to assign the default values. There's a good reason for this: various applications have their own default values and we might get confused when we switch to another application. To be on the safe side, we should always assign a value to a variable manually as soon as it is declared.

Got A Question?

'I love being Prime'

Have a question but don't want to ask it publicly? If you are a prime member just ask right here and we will get back to you within 48 hours. Not prime yet? no worries you can ask publicly below.

Act now while we have available seating

Our on line school is private, affordable and interactive with trainer support.
Act now save your seat before someone else takes yours.
Act now