-3

If java is pass by value, then why p1.age is 25 instead of 22? Please provide an explanation.

Here is the code:

class Person {
    Person(String s, int i) {
        ++pid;
        name=s;
        age=i;
    }

    static int pid;
    int age;
    String name;
}

public class Test {
    public static void main(String args[]) {
        Person p1 = new Person("John", 22);
        Test te = new Test();
        Person p2 = te.change(p1);
        System.out.println(p2.pid + " " + p2.name + " " + p2.age);
        System.out.println(p1.pid + " " + p1.name + " " + p1.age);

    }

    private Person change(Object o) {
        Person p2 = (Person) o;
        p2.age = 25;
        return p2;
    }
}

Output :
1 John 25
1 John 25

  • 1
    *java is pass by value*, who said that? This case you just passed the object. p1 and p2 are the same object. – hata Feb 20 '21 at 05:37
  • https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html – D.B. Feb 20 '21 at 05:39
  • Actually, he passed the reference 'p1' to the method. The reference got copied, but is still pointing to the exact same object. So by returning 'p2', you actually return a copy of a reference, but both references still point to the same object. – JayC667 Feb 20 '21 at 05:40
  • 2
    You create only a single `Person` object. You then just pass the reference to that one object around, renaming it and casting it, but never actually copying the object that the reference points to. There's only one `Person` object here. To have it be otherwise, you'd have to do something explicit to make a copy of the object or create a new one with another `new Person()`. – CryptoFool Feb 20 '21 at 05:40
  • In Java, primitive types are pass by value, objects on the other hand are pass by reference. – SJaka Feb 20 '21 at 05:43
  • 2
    @SJaka a better way to put it is that you can't pass objects in Java, only values, which can be references to objects. – tgdavies Feb 20 '21 at 05:45
  • 2
    @hata "_Who said that?_" – https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value. In the case of non-primitives, the value passed is the reference (i.e. the reference is copied); the object instance itself, however, is not copied. – Slaw Feb 20 '21 at 05:47
  • @tgdavies, correct. But I've seen some people who are trying to learn this concept gets confused with it. Making the distinction between primitive types vs. objects makes it easier to some. – SJaka Feb 20 '21 at 06:37
  • 1
    @SJaka Even objects are _pass-by-value_. If they were _pass-by-reference_ then you would be able to change a caller's reference by reassigning a parameter in the called method. – Slaw Feb 21 '21 at 04:22
  • @Slaw So when an object is sent as a parameter to a method, is `value` of the reference is sent or a copy of the object? I understand what you are saying, just saying that for a beginner, a copy of a primitive's value vs. a copy of an object's reference is sometime confusing. – SJaka Feb 22 '21 at 06:41

1 Answers1

0

You can try this code

   class Person {
    static int pid;
    int age;
    String name;
    Person(String s, int i) {
        ++pid;
        name=s;
        age=i;
    }
    Person(){};
}

public class Test {
    public static void main(String args[]) {
        Person p1 = new Person("John", 22);
        Test te = new Test();
        Person p2 = te.change(p1);
        System.out.println(p2.pid + " " + p2.name + " " + p2.age);
        System.out.println(p1.pid + " " + p1.name + " " + p1.age);

    }

    private Person change(Person o) {
        Person p2 = new Person();
        p2.name="John";
        p2.age=25;
        return p2;
    }
}
iamdhavalparmar
  • 1,090
  • 6
  • 23