Published 19 Sep, 2022

Java - Why is it possible to declare a variable without an initial value?

Category Java
Modified : Oct 04, 2022
67

I'm reading Gilles Dowek's Principles of Programming Languanges:

He says that it's also possible to declare a variable without giving it an initial value and also that we must be careful not to use a variable which has been declared without an initial value and that has not been assigned a value. This produces an error.

Note: The book's author mentions the possibility of declaring variables without an initial value on Java.

So, why is this declaration of variables valid? When am I going to use it?

Answers

There are 4 suggested solutions here and each one has been listed below with a detailed description. The following topics have been covered briefly such as Java, Variables, Declaration. These have been categorized in sections for a clear and precise explanation.

28

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.


43

Ok here is an example. I am currently setting global values that are set to persistent on a PDF forums. For this application I want to uses these values as data storage much like a text field. Not the best method but it works for this example. Rather than populated my forum with hundreds of hidden text fields I can set the values as a persistent global variables.

If my initial deceleration is set to 0 or "" as a value of the variable than it will not keep data between sessions. The initialization of the forum will reset the value to nothing. Much like Scrips, forums operated in order. It will recognize all of the scripts in the document first, before assigning values to carry over variables. Thus it is necessary to declare the variable without a value, so that the initialization does not replace the values.

In general it is also good house keeping to declare all of your variables at the top. It is possible to declare them as you go, but I have found that when a new element is added to a Script I have to move lines up and down to keep my code clean. If the variable is declared when it is used, than it also becomes very easy to use a variable before it is declared, which does not work.

Additionally

It is efficient to assign value when declaring, But not generic place holder values. In most scripts int is not as simple as saying int x = 5; More often we call on data such as this.getField("TextBox3").value; or some other line code. By assigning a generic value such as "" or 0 or true your function may work even if you failed to properly utilize a value. By not assigning a value, if you fail to utilize your variable, than your function will fail allowing you to narrow the possibilities when problem solving. If int x; is still NaN by the time you utilize x than your data collection and validations are likely the source of the problem.

In general declare all values at the top and avoid setting generic values unless you have to. If you are going to define the values of x later than don't set it to int x = 0;.


42

I am new to c++, but I think this information may also be helpful.

FROM: Programming: Principles and Practice Using C++ :

Why does C++ offer both declarations and definitions? The declaration/definition distinction reflects the fundamental distinction between what we need to use something (an interface) and what we need for that something to do what it is supposed to (an implementation). For a variable, a declaration supplies the type but only the definition supplies the object (the memory). For a function, a declaration again provides the type (argument types plus return type) but only the definition supplies the function body (the executable statements). Note that function bodies are stored in memory as part of the program, so it is fair to say that function and variable definitions consume memory, whereas declarations don’t.


39

Something like:

//declare without giving value.
int i;

//set value
i=9;