DEV Community

loading...

Discussion on: How Python Sees Variables

mcscodes profile image
Mcvean Soans Author

In Java, null is just a value which a variable can have. It means that the variable refers to nothing, but still space is consumed for the "reference".

Java having two types of memory: stack and heap, primitive types (like int, float), and object references in Java are stored in the stack memory. Objects themselves are stored in the heap memory. Hece, if two int variables contain the same value they will both be stored on the stack.

In Python however there is no separation, and everything is considered to be an object, and every object is stored in the heap memory. If two int variables have the same value, Python optimizes memory on the heap by allowing the variables or "tags" to point to the same object on the heap.

For more info on memory management, you may kindly check out my previous post Python: Memory Management, or a GeeksforGeeks article on Java Memory Management.

Thread Thread
alainvanhout profile image
Alain Van Hout

Yes, primitives are different, but quite a lot in Java is also 'just an object'. That primitives are different is also apparent in the fact that they can't be null.