Last active
January 30, 2016 01:41
-
-
Save codinko/c55019cadac749f6006a to your computer and use it in GitHub Desktop.
Java is pass-by-value, and not pass-by-reference
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Pass By Value | |
In Java, all parameters are passed by value, i.e. assigning a method argument is not visible to the caller. | |
*/ | |
Example 1: | |
public class PassByValueString { | |
public static void main(String[] args) { | |
new PassByValueString().caller(); | |
} | |
public void caller() { | |
String value = "Nikhil"; | |
boolean valueflag = false; | |
String output = method(value, valueflag); | |
/* | |
* 'output' is insignificant in this example. we are more interested in | |
* 'value' and 'valueflag' | |
*/ | |
System.out.println("output : " + output); | |
System.out.println("value : " + value); | |
System.out.println("valueflag : " + valueflag); | |
} | |
public String method(String value, boolean valueflag) { | |
value = "Anand"; | |
valueflag = true; | |
return "output"; | |
} | |
} | |
Result | |
output : output | |
value : Nikhil | |
valueflag : false | |
Example 2: | |
/** * * Pass By Value * */ | |
public class PassByValueNewString { | |
public static void main(String[] args) { | |
new PassByValueNewString().caller(); | |
} | |
public void caller() { | |
String value = new String("Nikhil"); | |
boolean valueflag = false; | |
String output = method(value, valueflag); | |
/* | |
* 'output' is insignificant in this example. we are more interested in | |
* 'value' and 'valueflag' | |
*/ | |
System.out.println("output : " + output); | |
System.out.println("value : " + value); | |
System.out.println("valueflag : " + valueflag); | |
} | |
public String method(String value, boolean valueflag) { | |
value = "Anand"; | |
valueflag = true; | |
return "output"; | |
} | |
} | |
Result | |
output : output | |
value : Nikhil | |
valueflag : false | |
Example 3: | |
/** This 'Pass By Value has a feeling of 'Pass By Reference' | |
Some people say primitive types and 'String' are 'pass by value' and objects are 'pass by reference'. | |
But from this example, we can understand that it is infact pass by value only, keeping in mind that here we are passing the reference as the value. ie: reference is passed by value. That's why are able to change and still it holds true after the local scope. But we cannot change the actual reference outside the original scope. what that means is demonstrated by next example of PassByValueObjectCase2. | |
*/ | |
public class PassByValueObjectCase1 { | |
private class Student { | |
int id; | |
String name; | |
public Student() { | |
} | |
public Student(int id, String name) { | |
super(); | |
this.id = id; | |
this.name = name; | |
} | |
public int getId() { | |
return id; | |
} | |
public void setId(int id) { | |
this.id = id; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
@Override | |
public String toString() { | |
return "Student [id=" + id + ", name=" + name + "]"; | |
} | |
} | |
public static void main(String[] args) { | |
new PassByValueObjectCase1().caller(); | |
} | |
public void caller() { | |
Student student = new Student(10, "Nikhil"); | |
String output = method(student); | |
/* | |
* 'output' is insignificant in this example. we are more interested in | |
* 'student' | |
*/ | |
System.out.println("output : " + output); | |
System.out.println("student : " + student); | |
} | |
public String method(Student student) { | |
student.setName("Anand"); | |
return "output"; | |
} | |
} | |
Result | |
output : output | |
student : Student [id=10, name=Anand] | |
Example 4: | |
/** | |
In addition to what was mentioned in Example3 (PassByValueObjectCase1.java), we cannot change the actual reference outside the original scope." | |
Note: I am not pasting the code for private class Student. The class definition for Student is same as Example3. | |
*/ | |
public class PassByValueObjectCase2 { | |
public static void main(String[] args) { | |
new PassByValueObjectCase2().caller(); | |
} | |
public void caller() { | |
// student has the actual reference to a Student object created | |
// can we change this actual reference outside the local scope? Let's see | |
Student student = new Student(10, "Nikhil"); | |
String output = method(student); | |
/* | |
* 'output' is insignificant in this example. we are more interested in | |
* 'student' | |
*/ | |
System.out.println("output : " + output); | |
System.out.println("student : " + student); // Will it print Nikhil or Anand? | |
} | |
public String method(Student student) { | |
student = new Student(20, "Anand"); | |
return "output"; | |
} | |
} | |
Result | |
output : output | |
student : Student [id=10, name=Nikhil] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment