fork download
  1. /**
  2. * Java pass by value!
  3. * Based on https://w...content-available-to-author-only...v.com/3884/java-is-pass-by-value-and-not-pass-by-reference
  4. */
  5.  
  6. import java.util.*;
  7. import java.lang.*;
  8. import java.io.*;
  9.  
  10. class Ideone
  11. {
  12. public static void main (String[] args) throws java.lang.Exception
  13. {
  14. System.out.println("Creating an object called red with an internal property color 'red'");
  15. Balloon red = new Balloon("red");
  16. System.out.println("Object reference of red: "+red.toString());
  17. System.out.println("Creating an object called blue with an internal property color 'blue'");
  18. Balloon blue = new Balloon("blue");
  19. System.out.println("Object reference of blue: "+blue.toString());
  20.  
  21. System.out.println("\nChanging red color field to uppercase...");
  22. System.out.println("Original red color: "+red.getColor());
  23. upperCaseColor(red);
  24. System.out.println("Outside of the upperCaseColor method the color is : "+red.getColor());
  25. System.out.println("One may think, it is working on the reference!");
  26. System.out.println("But actually it is working on a copy of the reference...");
  27.  
  28. System.out.println("\nSwapping objects red by blue...");
  29. swap(red, blue);
  30. System.out.println("But outside swap method, red still reffers to red: "+red.toString());
  31. System.out.println("And outside swap method, blue still reffers to blue: "+blue.toString());
  32. System.out.println("If b1 and b2 would dealing with references of red and blue, swap method would change the original references.");
  33. 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.");
  34. }
  35.  
  36. public static void swap(Balloon b1, Balloon b2) {
  37. System.out.println("Swap received b1 referring to red: "+b1.toString());
  38. System.out.println("Swap received b2 referring to blue: "+b2.toString());
  39. Object temp = b1;
  40. b1=b2;
  41. b2=(Balloon)temp;
  42. System.out.println("Inside swap method b1 now refers to blue: "+b1.toString());
  43. System.out.println("Inside swap method b2 now refers to red: "+b2.toString());
  44. }
  45.  
  46. public static void upperCaseColor(Balloon b) {
  47. System.out.println("The method upperCaseColor has a reference of the ballon object, its color is: "+b.getColor());
  48. b.setColor(b.getColor().toUpperCase());
  49. System.out.println("After uppercase the color of the object it has reference: "+b.getColor());
  50. }
  51. }
  52.  
  53. class Balloon {
  54. private String color;
  55.  
  56. public Balloon(){}
  57.  
  58. public Balloon(String c){
  59. this.color=c;
  60. }
  61.  
  62. public String getColor(){
  63. return color;
  64. }
  65.  
  66. public void setColor(String color) {
  67. this.color = color;
  68. }
  69. }
Success #stdin #stdout 0.14s 36008KB
stdin
Standard input is empty
stdout
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.