You will be able to understand...
- Is Java pass by values or pass by reference ?
- How memory scope does works for methods and members ?
Important Note: Java is pass by value.
What is the output from the following code?
public class Question2 {
public static void main(String[] args) {
int a = 5;
int b = 7;
swap(a,b);
System.out.println(a + ", " + b);
}
public static void swap(int a, int b) {
int temp = a;
a = b;
b = temp;
}
}
A) 5, 7
B) 7, 5 //This response is incorrect. Java is pass by value, so the swap method only swaps the parameters a and b, not the original values of a and b in main.
Output: 5, 7
Java Memory Models
key points:
- main method's scope
- Constructor's scope
- The Heap
(what about getter, setter, static, class?)
Example:
1.

2.- What is the output here?

3.

4.

5.

6.

7.
