fork(65) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main( String[] args ){
  11. Dog aDog = new Dog("Max");
  12. foo(aDog);
  13.  
  14. if (aDog.getName().equals("Max")) { //true
  15. System.out.println( "Java passes by value." );
  16.  
  17. } else if (aDog.getName().equals("Fifi")) {
  18. System.out.println( "Java passes by reference." );
  19. }
  20. }
  21.  
  22. public static void foo(Dog d) {
  23. d.getName().equals("Max"); // true
  24.  
  25. d = new Dog("Fifi");
  26. d.getName().equals("Fifi"); // true
  27. }
  28. }
  29. class Dog{
  30. private String name;
  31.  
  32. public Dog(String name){
  33. this.name = name;
  34. }
  35.  
  36. public String getName(){
  37. return this.name;
  38. }
  39. public void setName(String name){
  40. this.name = name;
  41. }
  42. }
Success #stdin #stdout 0.1s 320576KB
stdin
Standard input is empty
stdout
Java passes by value.