[JAVA] Question about data flow betwean methods

  • Context: Java 
  • Thread starter Thread starter mindauggas
  • Start date Start date
  • Tags Tags
    Data Flow Java
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
4 replies · 2K views
mindauggas
Messages
127
Reaction score
0
Hi,

Wanted to ask you guys how does Java know what specific parameter to take into a method if the method name has to be only - let's call it - "internaly consistent" (meaning the parameter in the head and the body have to be the same).

E.g. if I pass a value to the method:
Code:
private int rollDice(int numDice)
that uses a for loop:
Code:
for (int i = 0; i < numDice; i++)
So here the parameter has to have the same name - internaly consistent.

In the main method this program uses, as an example, the line:

Code:
while (true) {
	int roll = rollDice(numDice);

Now information flow can be analysed in terms of "stack frames". Each method get's its own stack frame. But the parameters it accepts from the method-caller are only copies of the parameters (values) not the values themselves. If the names betwean methods have to be consistent only internaly - inside the method - and are relative method-vise how does JAVa realize what parameter I'm referring to?

I hope the question makes sense.
 
on Phys.org
Hey mindauggas.

In normal compiler environments (not virtual machines) you have two types of variable passing: by reference and by value.

By reference provides a memory location and by value loads all the data on the stack.

With regard to the VM, you will need to read on how things are passed and how they are referenced.

Some things to consider is whether symbol look-ups are used within a given context (i.e. within a particular function scope where you have a stack-frame for that function) or whether there is an internal addressing procedure that passes a pseudo-memory location for calling by reference and provides a limited stack with run-time type information (RTTI) for calling by value.

You would find this in the compiled information for Java Byte-code representation and along with telling you how the Java VM is likely to actually work specifically, it will tell you everything is translated specifically with regards to how things are actually accessed.

Personally I think it's going to be a combination of a look-up table and some kind of function context object that allows an extremely quick look up and translation of a variable to its intended format usage.

Take a look at optimized ways of symbolic lookup in the context of interpreted environments (like a VM) for more information.
 
I see that this is ca complex question. Definitely will pursue it. Thank you very much chiro.

Also, if anyone has any additional details to provide it would be appreciated.
 
Since numDice is a pass by value parameter that is copied and pushed onto the stack for each instance of rollDice(), Java probably translates each instance of numDice in rollDice(...) into a fixed offset relative to the current stack pointer, instead of an absolute address. There would only be one entry in the symbol table for method rollDice, parameter numDice, that fixed offset relative to the stack pointer.
 
Last edited: