Why is it possible to declare a variable without an initial value?
There are many different reasons for many different languages.
MEMORY
When you declare a variable, you want some memory to hold the variable in. This involves asking the kernel of the operating system for memory, or some kind of monitoring program which keeps track of memory. In short, this can be an expensive operation.Hence, in many cases, it is desirable to allocate all the memory required for the object at the same time, and then assign whatever value has to be assigned to it later. This way, you can increase the performance of the program in the critical parts. This use case is common enough that a feature allowing declaration without initialization is allowed. However, good practices assert that in all other cases you should initialize the variable while assigning.
Think of the memory allocation as a bureaucracy. There is too much paper work. So, if you know you are going to use a large amount of memory later, you ask for a large amount of memory upfront in one single transaction, rather than asking the kernel each next time.
EXPENSIVE INITIALIZATION
This point is very similar to the above point. Suppose you have a 1 million times 1 million array. Initializing such an array is an expensive procedure. To do so with defaults would be stupidity, and hence, such a feature, where memory is allocated and then used as needed.
In here, its like you are buying a huge amount of lego blocks to construct something, but you want to buy them in shapes of the default spiderman. The shopkeeper or you would have to extra hard to get them in shapes of spiderman when you are anyway going to reshape them later.
For example, you could have something like this:
int i;
if (first_condition)
i = 1;
elseif (second_condition)
i = 2;
else
i = 0;
Your variable would need to be declared outside the if
for it to be used later, but its value is fixed inside the if
conditions.
If you really look at what happens when you declare a variable and initialize it (assign an initial value), you will see that down at machine instruction level, or byte code for Java there is a significant amount of computational power being used in both these steps.
- declaring the variable means allocating memory, creating an instance of asked type, etc etc
- Then initializing that location of memory, again requires more processing to move the default value to the allocated memory location, like filling it with 0s if the default value is 0. (Note: when random memory location is allocated to a variable that memory could contain any pattern, left there by a previous value)
So if user unknowingly use a variable without first giving it an acceptable value for its type, then there could be an error if the value which was there was not correct.
So if a language forces you to initialize a variable (or does so by itself) at declaration then it reduces chances for an error down the line, but it may be wasting processing power for something you didn't really want.
On the other hand, if it allows you to declare a variable without initializing it, it gives you the control and may be saving some computing power for you but open the chances for an error. (Assume you have a scenario where your initial value for the varible depends on some other conditions, which are going to consider and assign the variable accordingly. In such case initializing the var at declaration could be just waste processing power).
Languages decide which path they want to take, mostly depend on what they consider their power is.
if it is about giving the programmer a chance to have control, and make highly optimized programs then they will usually allow declaring vars without initializing, in addition to more stuff.
But if the language is about forcing to programmer to write more error free programs it would take the other path.