Why is it necessary for variables to be initialized with default values ​​in Java

In the article on how objects are initialized in Java, a paragraph was given that follows:

At the beginning of an object’s life, the Java Virtual Machine (JVM) allocates enough memory on the heap to host the instance variables. However, when this memory is first allocated, the data contained in it is unpredictable. If memory was used as is, the behavior of the object would also be unpredictable. To protect against such a scenario, Java ensures that the memory is initialized, with the least predictable defaults, before it is used by any code.

Can anyone explain what is meant by unpredictable data and unpredictable behavior here. thanks in advance

+7
source share
4 answers

Can anyone explain what is meant by unpredictable data and unpredictable behavior here.

If you programmed in C / C ++, you would have noticed that an uninitialized variable carries some garbage value that is present in the allocated memory cell, it is interpreted as the data type of the variable. The compiler does not complain about such variables, and if the developer forgets to initialize them correctly, garbage values ​​will be used, which will lead to unexpected behavior of the program.

In Java, the JVM initializes all member variables to standard values ​​based on the data type of the variable and complains about local variables that are not initialized at compile time to avoid this unexpected behavior and force the developer to always use initialized variables.

+5
source

Because if you do not initialize the variable, the JVM will not understand what to assign, and it will take any value that will lead to unpredictable data.

The compiler never assigns a default value to an uninitialized local variable.

From Wikipedia:

There are no uninitialized variables in Java. Fields of classes and objects that do not have an explicit initializer and array elements are automatically initialized with a default value for their type (false for boolean, 0 for all numeric types, null for all types of links). Local variables in Java must be definitely assigned before they are available, or it is a compilation error.

+4
source

This unpredictability can be tested in C or C ++, where the language does not automatically initialize the variables. Here is one example from Wikipedia:

 void count( void ) { int k, i; for (i = 0; i < 10; i++) { k = k + 1; } printf("%d", k); } 

http://en.wikipedia.org/wiki/Uninitialized_variable

We do not know k , because we do not know what meaning it originally had. The initial value is what is already in the allocated memory block: 0x0A4C1330, or 0x00000000, or 0x00FF3333, etc. As a result, unpredictable data gives (all kinds) unpredictable behavior.

+4
source

In C and other languages ​​compiled for native code and running in an uncontrolled environment, an uninitialized variable can contain any random value. This is exactly what the term unpredictable means. Think of a pointer pointing to an unknown place in memory. If your program mistakenly starts using this pointer, it at least behaves unpredictably, if this value is used for reading, it may crash, because, for example, casting the value located in this cell of a random cell cannot be discarded in the type written in the code, or even cause system damage if one program writes information to a memory area, belongs to another program.

+4
source

All Articles