Why is it necessary that variables should be initialized to default values in Java
Asked Answered
P

4

7

In an article about how objects are initialized in Java there was a paragraph which is given below:

At the beginning of an object's life, the Java virtual machine (JVM) allocates enough memory on the heap to accommodate the object's instance variables. When that memory is first allocated, however, the data it contains is unpredictable. If the memory were used as is, the behavior of the object would also be unpredictable. To guard against such a scenario, Java makes certain that memory is initialized, at least to predictable default values, before it is used by any code.

Can anyone kindly explain that what is meant by unpredictable data and unpredictable behavior here. Thanks in advance

Puccini answered 3/11, 2012 at 6:30 Comment(0)
P
6

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

If you programmed in C/C++, you would notice that an uninitialized variable carries some garbage value, present in the memory location allocated to it, interpreted as per the data type of the variable. The compiler doesn't complain about such variables and if the developer forgets to initialize them properly, the garbage values gets used, resulting in unexpected behavior of the program.

In Java, the JVM initializes all member variables to the default values based on the data type of the variable and complains about local variables that are not initialized during compilation, to avoid such unexpected behavior and making the developer always use initialized variables.

Pilar answered 3/11, 2012 at 6:36 Comment(0)
H
4

Because if you dont initialize the variable then JVM will not understand what to assign and it will take any value which will result in unpredictable data.

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

From Wikipedia:

Java does not have uninitialized variables. Fields of classes and objects that do not have an explicit initializer and elements of arrays are automatically initialized with the default value for their type (false for boolean, 0 for all numerical types, null for all reference types).Local variables in Java must be definitely assigned to before they are accessed, or it is a compile error.

Haihaida answered 3/11, 2012 at 6:32 Comment(0)
O
4

This unpredictability can be experienced in C or C++ where the language does not automatically initializes variables. Here's 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 don't know k because we don't know what value it held initially. The initial value is whatever is already contained in allocated the block of memory: 0x0A4C1330, or 0x00000000, or 0x00FF3333, etc. As a consequence, unpredictable data yields (all kinds of) unpredictable behaviour.

Olympium answered 3/11, 2012 at 6:38 Comment(0)
H
4

In C and other languages compiled to native code and running in non-controlled environment the not-initialized variable can hold any random value. This is exactly what term "non-predictable" means. Think about pointer that points to some unknown location in memory. If your program by mistake starts using this pointer it at least behave unpredictably if this value is used for reading, it may crash because for example casting of value located at that random memory cell cannot be cast to type written in code or even cause system corruption if one program writes information to memory region belongs to another program.

Handgrip answered 3/11, 2012 at 6:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.