0

I was reading an article that said everything is an object in Python.

So I thought of researching how exactly integers in Python are stored internally, but unfortunately I got no good answer.

My question is that do integer variables in Python hold pointers to integer objects in memory, or do they simply point to the integers, as is, in memory.

Comparatively, how are integer variables stored in Java? Are they also pointers to integer objects in memory or to integers, as is, in memory?

coderboy
  • 1,710
  • 1
  • 6
  • 16
  • Answer for [java](https://stackoverflow.com/questions/5277881/why-arent-integers-cached-in-java) and [python](https://stackoverflow.com/questions/306313/is-operator-behaves-unexpectedly-with-integers). TLDR: A variable is similar to a pointer pointing to an object. Note that its not exactly a pointer as pointer arithmetic is not supported in Python or C++. – Anmol Singh Jaggi Sep 09 '20 at 15:37

1 Answers1

0

From my understanding, in python, an int object is created, and then the variable holds a reference that points to this object. Here is a little bit of reading you can do.

In java, the integers are primitive types. So the variables do not hold memory locations like reference types. Rather, they hold the value of the integer itself. The wrappers for the primitive types are reference types. Here is a good article for java

fooiey
  • 1,040
  • 10
  • 23
  • I think that internally a variable has to hold a memory location - it's the only way the computer can retrieve the value stored in the variable. So with that in mind maybe variables in Java also contain memory locations to integers, but these locations contain the integers as is, unlike in Python, where these memory locations contain the integers as objects. – coderboy Sep 09 '20 at 15:55
  • 1
    Well yes, all variables are memory locations. What is stored at that memory location is different. Lets say both of these variables are on the stack. For a primitive type, at the memory location is the value of that primitive type. For a reference type, it stores a pointer to a memory location on the heap that stores the object. – fooiey Sep 09 '20 at 17:01
  • That sounds convincing! – coderboy Sep 10 '20 at 06:21