Java [JAVA] Question about data flow betwean methods

  • Thread starter Thread starter mindauggas
  • Start date Start date
  • Tags Tags
    Data Flow Java
AI Thread Summary
Java determines which parameter to use in a method by relying on the method's stack frame, where each method call has its own context. Parameters are passed by value, meaning that copies of the values are pushed onto the stack, allowing Java to maintain internal consistency with parameter names. The language utilizes symbol look-ups within the function scope to manage variable references, which may involve a combination of look-up tables and context objects for efficient access. When a method is called, Java translates the parameter references into fixed offsets relative to the stack pointer, rather than using absolute addresses. Understanding these mechanisms provides insight into Java's data flow and method parameter handling.
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.
 
Technology news 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:
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top