You have been learning about the primitive variable in Java in this tutorial and this tutorial, the reference variable in this tutorial. In this tutorial, we will learn more about these two types of variables when they are passed to a method in the form of parameters.
Passing the primitive variable into a method
When we pass a primitive variable to a method, its value is copied and passed to that method. This means that the value of this primitive variable will not change at the end of the execution of the method, although it is possible that during the execution of this method we will change its value.
Consider the following example code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.huongdanjava; public class Example { private int number; public void increase(int n) { n = n + 1; System.out.println(n); } public static void main(String[] args) { Example e = new Example(); System.out.println(e.number); e.increase(e.number); System.out.println(e.number); } } |
Result:
1 2 3 |
0 1 0 |
In this example, we passed for the increase() method in the Example class a primitive n type of int. This method has increased the value of the primitive variable that we pass into. But obviously you see, the value of the number variable in the Example object does not change after being passed to the increase() method.
Passing the reference variable to a method
Assume, I have class Student with the following content:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.huongdanjava; public class Student { private String name; public Student(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } } |
Now we will make an example to use this Student object in a method like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.huongdanjava; public class Example { public void swap(Student s1, Student s2) { Student temp = s1; s1 = s2; s2 = temp; } public static void main(String[] args) { Student student1 = new Student("Khanh"); Student student2 = new Student("Thanh"); Example e = new Example(); e.swap(student1, student2); System.out.println(student1.getName()); System.out.println(student2.getName()); } } |
In the above example, I used the Student object in a swap() method with the intention of swapping references of two variables. Take a look at the following:
1 2 |
Khanh Thanh |
As you can see, the reference to the student1 and student2 variables is still unchanged after the swap() method is executed.
But if you change the state of the Student object, please consider the following example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.huongdanjava; public class Example { public void changeName(Student s, String name) { s.setName(name); } public static void main(String[] args) { Student student = new Student("Khanh"); Example e = new Example(); e.changeName(student, "Thanh"); System.out.println(student.getName()); } } |
Result:
1 |
Thanh |
You see, if you change the state of the object, after the method is executed, the state of the object will change.