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

Asked

Viewed 332 times

3

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

  • I answered what I gave, I don’t know what you mean by behavior.

  • I have now noticed that you have never accepted an answer to the questions you have asked. It is good practice to accept answers if they have solved your problem...

  • You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? Something needs to be improved?

2 answers

5

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

Both class-based variables - which are references - and so-called primitives - which are by value - may be in stack or in the heap. Depends on where she belongs.

Variables by reference have only 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 the stack, otherwise you will be in the heap. Except that these variables, unless they are null, always point to another object in memory. This object is always in the heap (except for some internal optimization).

In the case of variables by value, the primitives (actually type by value, because this will generate confusion when Java has, and have, types by non primitive value), 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, a array), then obviously she’ll be in heap, after all, the object that owns it is already in the heap.

Already I answered that to C#. Is very similar.

Initialization of variables will always generate a value default predefined by the language. No matter the type. The value is always 0 and each type can treat that zero differently. In the case of reference types it will be null, since this type is a pointer, but it is still a beautiful zero.

Only one constructor - even if default - can put value on the object pointed by the reference, ie, need to build the object and assign its address to the variable. The internal members of this type will be initialized according to the established class, and can, of course, 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.

1

Is not possible nay 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
    }
}

Upshot:

0
null

For a primitive class variable, it is initialized automatically 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. Notice the lines that say //erro de compilação, if you try discolour these lines and compile the code.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.