What is the behavior of the reference variable and the primitive variable?

3

What is the behavior of a variable of type reference (null) and primitive type (0) in memory before being initialized. Where are they in memory?

    
asked by anonymous 31.08.2015 / 16:08

2 answers

3

First understand that variable is a memory address you gave a name to.

Both class-based variables - which are references - and primitive calls - that are by value - may be in < in or heap . It depends on where it belongs.

The variables by reference only have pointers and they can be in both areas of memory depending on the need. If the variable is local to a method, it will be in stack , otherwise it will be heap . But these variables, unless they are null, always point to another object in memory. This object is always in heap .

In the case of variables by value, primitives, the object itself is in the location of the variable. If it was declared locally in a method it will be in the stack . But if it belongs to another object by reference (a class, an array ), then obviously it will be in heap , after all, its owner object is already heap .

I already have answered this for C # . It's very similar.

The initialization of the variables will always generate a pre-defined default language. It does not matter the type. The value is always 0 and each type can treat that zero in a different way. In the case of types by reference will be null, since this type is a pointer, but it is still a beautiful zero.

Only a constructor - even if it is the default - can put value in the object pointed to by the reference, that is, it needs to construct the object and assign its address to the variable. The internal members of this type will be initialized according to the established in the class, and, of course, can be what comes from the constructor. Some people think this is the value of the variable, but conceptually speaking, it is not. Of course we use the term the wrong way in everyday life and everyone understands.

Then it is possible not to initialize the object by reference but all variables will be initialized with a value.

    
31.08.2015 / 16:15
1

Can not not initialize a primitive variable. See the test:

public class Teste {

    static int iInstancia;          //inicializada automaticamente, com 0
    static Integer objInstancia;    //não-inicializada

    public static void main(String[] args) {
        int iLocal;
        Integer objLocal;
        System.out.println(iInstancia);
        System.out.println(objInstancia);
        //System.out.println(iLocal); //erro de compilação
        //System.out.println(objLocal); //erro de compilação
    }
}

Result:

  

0
  null

For the case of a primitive variable of the class, it is automatically initialized with a default value. For a method variable, regardless of whether it is primitive or reference, it must be initialized, otherwise the code does not compile. Note the lines that say //erro de compilação , if you try to uncomment these lines and compile the code.

Already where they are in memory, it is well answered by the bigown.

    
31.08.2015 / 16:19