what is the difference between “value” and “reference for the value” , and are we store the references or the values in the varibales ?
A variable is just a nice name which, at run time, means some fixed memory address (fixed as long as this variable lives, for instance during one execution of a given method, if this is a local variable).
In the JVM world, the memory spot which the variable symbolizes can only contain a 32 bits word. More precisely, this address either corresponds to a spot inside a stack frame (local variable) or to a field in an object (attribute); both have a very constrained structure where all storage units are exactly 32 bits.
A 32 bits word is enough for most primitive types, but not for complex objects. So when a variable is meant to access such an object, rather than putting the object directly at this memory spot, the (pseudo-)address of the object is stored instead. Fortunately, the object itself is stored in the heap, which is a much less structured memory area where objects of arbitrary size can be stored.
In the latter case, it can be said that the variable contains a reference to the value, whereas for primitive types, the variable would directly contain the value. So the answer to your second question would be “it depends”.
I hope this is clear enough?
Thx bro , so what I got it that :
When I create a variable and initialize it with a value , I allocate space in the memory and I put the value itself in that space , but that working only for primitive types because primitive types doesn’t need so mush memry , but when comes to an complex object , like string , we put the address for the object in the memory not the object itself , so we can say that the variable has a value when we talk about the primitive types and we say the variable has a reference to a value if we talking about complex object ?
and if we has a reference for the object , where is the object itself ?
sorry bro if there are a Grammer mistakes in englisg
After some research , I knowed that Java divide the RAM into 3 Memory Segments
Code segment , stack segment and heap segment , are the Kotlin like Java ?
This memory organization is how it works with the JVM, so all JVM languages (Java, Scala, Groovy, Kotlin/JVM) are concerned. However:
- similar organization exists for other platforms (but the constraint of storing only 1-word values in the stack and in fields is a JVM choice which is not that common; for instance, instructions of most native platforms handle adresses with no concern with where a “value” starts or ends)
- Kotlin tries to abstract away this distinction: there is no primitive types at language level; only in the compiled code when it makes sense. So Kotlin programmers should usually not worry about this. The access by reference thing only matters for mutable object: if you mutate a field, the change can be seen from all variables which were assigned the same object. Note there is no ambiguity concerning primitive-like values, since they do not have mutable fields to begin with.