Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

l-value and r-value, stack and heap

Tags:

java

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:

enter image description here

enter image description here

enter image description here enter image description here

like image 989
MrAP Avatar asked Feb 16 '17 15:02

MrAP


People also ask

What is an L value and an R value?

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.

What is the difference between L value and R value of a variable?

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.

Where are R values stored in memory?

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.

Which of the following is true for L values and R values?

d) l-values are local and r -values are relative.


1 Answers

  1. When the terms 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.
  2. 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.
like image 57
VHS Avatar answered Oct 26 '22 22:10

VHS