In general that's true enough, but e.g. when you declare a variable and at that time set it to be equal to an existing variable, then no new memory allocation happens (in Java I mean).
To be honest, I'm not sure whether there would be a memory allocation when you set a variable to null when declaring it.
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.
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.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
In general that's true enough, but e.g. when you declare a variable and at that time set it to be equal to an existing variable, then no new memory allocation happens (in Java I mean).
To be honest, I'm not sure whether there would be a memory allocation when you set a variable to null when declaring it.
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.
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.