Sobes.tech
Senior

Where is the primitive int (for example, the number 3) stored?

sobes.tech AI

Answer from AI

The primitive type int in Java is stored on the stack if it is a local variable of a method or a parameter. If the int is a field of an object, then it is stored in the heap as part of the object.

For example:

void method() {
    int a = 3; // 'a' is stored on the stack
}

class MyClass {
    int b = 3; // 'b' is stored in the heap inside the object
}

Thus, primitives are not objects and are not stored in the heap by themselves unless they are part of an object. Local variables of primitive types are always placed on the call stack.