0

I have trouble understanding the return types of a method in java. Where is it supposed to be? On the stack or heap? Let's take this example snippet:

/** the snippet returns the minimum between two numbers */
    public int minFunction(int n1, int n2) {
       int min;
       if (n1 > n2){
          min = n2;
       }else{
          min = n1;
          return min; 
          }
       }

how and where are minFunction, n1, n2, min stored in memory. if i call the method with the numbers 3 and 5 what will happen?

2
  • 2
    The actual method is stored in the Method Area. Parameters, return values, local variables,... are stored within a Method Frame, which in return resides on the Stack. Commented Oct 31, 2016 at 0:42
  • "if i call the method with the numbers 3 and 5 what will happen?" - it will return 3 (after the code is updated so it compiles). Commented Oct 31, 2016 at 0:45

1 Answer 1

-1

You have 2 options

  • You can store the returned value like this (for further uses)

    int minValue;
    minValue = minFunction(3,5)
    
  • or you can directly show it using a System.out.print() command like this

    System.out.println("the minimum value between 3 and 5 is" + minFuction(3,5));
    

In both cases, the returned value will be 3.

It doesn't get "stored" anywhere per se, but you CAN store it or just show it.

I hope I answered your question.

Sign up to request clarification or add additional context in comments.

2 Comments

"It doesnt get "stored" anywhere per se, but you CAN store it or just show it" - That is simply wrong. Even if you "just show" something, it has to be stored at some memory location to be shown. Your answer does in deed not answer the question. You should read up on heap- and stackmemory.
If it isn't stored anywhere it doesn't exist. Your answer doesn't even make sense.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.