* Based on https://w...content-available-to-author-only...v.com/3884/java-is-pass-by-value-and-not-pass-by-reference
*/
importjava.util.*;
importjava.lang.*;
importjava.io.*;
class Ideone
{
publicstaticvoid main (String[] args)throws java.lang.Exception
{
System.out.println("Creating an object called red with an internal property color 'red'");
Balloon red =new Balloon("red");
System.out.println("Object reference of red: "+red.toString());
System.out.println("Creating an object called blue with an internal property color 'blue'");
Balloon blue =new Balloon("blue");
System.out.println("Object reference of blue: "+blue.toString());
System.out.println("\nChanging red color field to uppercase...");
System.out.println("Original red color: "+red.getColor());
upperCaseColor(red);
System.out.println("Outside of the upperCaseColor method the color is : "+red.getColor());
System.out.println("One may think, it is working on the reference!");
System.out.println("But actually it is working on a copy of the reference...");
System.out.println("\nSwapping objects red by blue...");
swap(red, blue);
System.out.println("But outside swap method, red still reffers to red: "+red.toString());
System.out.println("And outside swap method, blue still reffers to blue: "+blue.toString());
System.out.println("If b1 and b2 would dealing with references of red and blue, swap method would change the original references.");
System.out.println("Conclusion: swap received a copy of the reference of red and a copy of the reference of blue and modified only those copies not the original objects references.");
}
publicstaticvoid swap(Balloon b1, Balloon b2){
System.out.println("Swap received b1 referring to red: "+b1.toString());
System.out.println("Swap received b2 referring to blue: "+b2.toString());
Creating an object called red with an internal property color 'red'
Object reference of red: Balloon@5caf905d
Creating an object called bue with an internal property color 'blue'
Object reference of blue: Balloon@87aac27
Changing red color field to uppercase...
Original red color: red
The method upperCaseColor has a reference of the ballon object, its color is: red
After uppercase the color of the object it has reference: RED
Outside of the upperCaseColor method the color is : RED
One may think, it is working on the reference!
But actually it is working on a copy of the reference...
Swapping objects red by blue...
Swap received b1 referring to red: Balloon@5caf905d
Swap received b2 referring to blue: Balloon@87aac27
Inside swap method b1 now refers to blue: Balloon@87aac27
Inside swap method b2 now refers to red: Balloon@5caf905d
But outside swap method, red still reffers to red: Balloon@5caf905d
And outside swap method, blue still reffers to blue: Balloon@87aac27
If b1 and b2 would dealing with references of red and blue, swap method would change the original references.
Conclusion: swap received a copy of the reference of red and a copy of the reference of blue and modified only those copies not the original objects references.