Two methods and their signatures
A method groups code under a signature: name, parameters and return. modifyPrimitive(int) is void; calculateTotal returns int with return.
A method groups code under a signature: name, parameters and return.
modifyPrimitive(int) is void; calculateTotal returns int with return.
A method groups code under a signature: name, parameters and return. modifyPrimitive(int) is void; calculateTotal returns int with return.
The light blue particle is the execution flow. In main, it sets x = 10.
When calling modifyPrimitive(x), it prepares the arguments with value 10.
The light blue particle is the execution flow. In main, it sets x = 10. When calling modifyPrimitive(x), it prepares the arguments with value 10.
The JVM uses the Call Stack: it pushes a new stack frame for the call.
Each stack frame isolates its parameters and local variables from the rest.
By pass-by-value, number receives a copy of 10, not the variable x.
The JVM uses the Call Stack: it pushes a new stack frame for the call. Each stack frame isolates its parameters and local variables from the rest. By pass-by-value, number receives a copy of 10, not the variable x.
Inside the method, number = 500 overwrites the local parameter with 500.
Does x change in main? Assigning 500 might seem to alter the caller.
Inside the method, number = 500 overwrites the local parameter with 500. Does x change in main? Assigning 500 might seem to alter the caller.
But pass-by-value is strict: 500 only touched the frame local copy.
When the method ends, its frame is popped and x in main stays untouched at 10.
But pass-by-value is strict: 500 only touched the frame local copy. When the method ends, its frame is popped and x in main stays untouched at 10.
Now main calls calculateTotal(x, 5): a frame is pushed with 10 and 5.
It computes total = 15 and return hands that value back to the caller.
The frame is popped and main stores 15 in res to print it.
Now main calls calculateTotal(x, 5): a frame is pushed with 10 and 5. It computes total = 15 and return hands that value back to the caller. The frame is popped and main stores 15 in res to print it.
Overloading defines several methods with the same name and different parameters.
In add(3.5, 2.5), the compiler checks the types and picks add(double, double).
Resolved before running: the flow goes straight to the chosen method.
Overloading defines several methods with the same name and different parameters. In add(3.5, 2.5), the compiler checks the types and picks add(double, double). Resolved before running: the flow goes straight to the chosen method.
The Call Stack follows LIFO order: the last frame in is the first one out.
Every argument travels by pass-by-value (copy), and return pops with the result.
The Call Stack follows LIFO order: the last frame in is the first one out. Every argument travels by pass-by-value (copy), and return pops with the result.