[JAVA] Question about data flow betwean methods

In summary, Java knows which parameter to take into a method based on the name and the parameter's location within the method. The name and location must be consistent within the method.
  • #1
mindauggas
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 realize what parameter I'm referring to?

I hope the question makes sense.
 
Technology news on Phys.org
  • #2
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
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
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 to [JAVA] Question about data flow betwean methods

1. How does data flow between methods in Java?

When a method is called in Java, any data that is passed in as parameters will be copied and given to the method to use. This means that the original data will not be affected by any changes made within the method. Any changes made to the data within the method will only be visible within the method itself. Once the method finishes executing, the data will be returned to the original calling method.

2. Can data be passed between methods without using parameters?

Yes, data can also be passed between methods in Java by using global variables. These variables are declared outside of any method and can be accessed by any method within the same class. However, it is generally considered better practice to pass data using parameters as it makes the code more modular and easier to debug.

3. Is it possible to return multiple values from a method in Java?

No, a method in Java can only return a single value. However, this value can be a complex data type such as an array or an object, which can contain multiple values. Alternatively, you can use global variables to store and access multiple values from different methods.

4. How does the return statement affect data flow between methods?

The return statement is used to send a value back to the calling method. This value can then be assigned to a variable or used in other operations. If a method does not have a return statement, its data flow will end when the method finishes executing. If a method has a return statement, the data flow will continue with the value being returned.

5. Can the order of method calls affect the data flow in Java?

Yes, the order of method calls can affect the data flow in Java. If one method depends on the output of another method, the first method should be called before the second method. If the order is reversed, the second method will not have the correct data to work with and may produce errors.

Similar threads

  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
7
Views
3K
  • Programming and Computer Science
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
877
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top