[JAVA] Question about data flow betwean methods

  • Java
  • Thread starter mindauggas
  • Start date
  • #1
127
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 realise what parameter I'm refering to?

I hope the question makes sense.
 

Answers and Replies

  • #2
chiro
Science Advisor
4,790
132
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.
 
  • #3
127
0
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.
 
  • #4
rcgldr
Homework Helper
8,710
538
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:

Related Threads on [JAVA] Question about data flow betwean methods

  • Last Post
Replies
4
Views
1K
Replies
1
Views
2K
  • Last Post
2
Replies
28
Views
2K
Replies
6
Views
3K
  • Last Post
Replies
1
Views
2K
Replies
2
Views
3K
  • Last Post
Replies
1
Views
2K
  • Last Post
Replies
3
Views
6K
  • Last Post
Replies
7
Views
2K
  • Last Post
Replies
2
Views
1K
Top