Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

[JAVA] Question about data flow betwean methods

  1. Dec 19, 2012 #1
    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 (Text):
    private int rollDice(int numDice)
    that uses a for loop:
    Code (Text):
    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 (Text):
    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.
     
  2. jcsd
  3. Dec 19, 2012 #2

    chiro

    User Avatar
    Science Advisor

    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.
     
  4. Dec 19, 2012 #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.
     
  5. Dec 19, 2012 #4

    rcgldr

    User Avatar
    Homework Helper

    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: Dec 19, 2012
  6. Dec 19, 2012 #5

    jedishrfu

    Staff: Mentor

Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: [JAVA] Question about data flow betwean methods
  1. Question about Java (Replies: 4)

Loading...