I am a high school student learning Java (in BlueJ environment).
Context
My book, while discussing pass by value and pass by reference mechanisms, uses the terms, stack and heap and also states that each unit in the memory (also known as variable) has a name, l-value and r-value where l stands for 'locator' or 'location' and r stands for 'read'. The name is used to identify the unit, the l-value stores the address of the unit and the r-value stores the actual value of the unit. In case of primitive datatypes, it store the actual value while in case of reference datatypes it store the address of the reference datatype which refers or points to it. When a function with parameters is called, the r-value of the actual parameters are copied into the r-value of the actual parameter. In case of primitive datatypes the actual value is copied while in case of reference datatypes the reference address is copied due to which, in the former case there is no change in the actual values while in the latter case there is change in the actual values.
My Questions
Now, i decided to learn more about this on the Internet. I found that the discussions on the Internet are not in conformity to my book. There l-value and r-value are said to be the value to the left hand side and the right hand side respectively of the assignment sign. I am confused.
What is the actual meaning of l-value and r-value and what does my book mean by stack, heap (I want a simple and easy to understand answer) and unit of memory. I found many questions on this site dealing with stack and heap but could not understand the answers there as they were very technical and as such i do not have so much of a technical knowledge. Also i would like to know where i can learn more about this
Here are the pages from my textbook:
An lvalue refers to an object that persists beyond a single expression. An rvalue is a temporary value that does not persist beyond the expression that uses it.
An lvalue is an expression that yields an object reference, such as a variable name, an array subscript reference, a dereferenced pointer, or a function call that returns a reference. An lvalue always has a defined region of storage, so you can take its address. An rvalue is an expression that is not an lvalue.
Conceptually, rvalues live on the "stack". If you get to their address, it is conceptually an address somewhere on the stack. If the address isn't taken at all, the entity may never really come into existence as long as the compiler sets up the correct instructions to have it appear as if it were created.
d) l-values are local and r -values are relative.
l-value
and r-value
were first coined, l and r
indeed meant left and right. That is, l-value originally meant left
hand side of the assignment and r-value meant right hand side of the
assignment. However, later on, they were revised to indicate
'locator' and 'read' respectively as your book suggests. The reason
was that programming languages like C have many operators (e.g.
address of operator &
) where the operand that appears to the right
hand side of the operator is still an l-value.stack
and heap
are areas in the memory. Stack is used to store
local variables and function calls. Heap is used to store objects
.
Heap is shared by all threads of your application while stack is
assigned to each thread.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With