0

Code:

class AB{
    int i=5;
}

class BC{
    public void test(AB a){
        a.i=10;
    }

}

public class ATest{

public static void main(String aa[]){
    AB a = new AB();
    //Base class variable value
    System.out.println(a.i);

    BC b = new BC();
    //Modifying the object "a"
    b.test(a);
    //Printing the base class object
    System.out.println(a.i);
}
}

// Output : 5
//          10

If it is pass-by-value, the output should have been 5 and 5

Pino
  • 7,468
  • 6
  • 50
  • 69
Arijeet Saha
  • 1,118
  • 11
  • 23
  • 4
    Objects (ie instances of classes) in Java are pass by reference, primitive types (`int` etc.) are pass by value. – Joachim Isaksson Apr 06 '13 at 17:43
  • 3
    Almost: objects are not passed by reference, object references are passed by value. That's an important difference. – Max Beikirch Apr 06 '13 at 18:07
  • 1
    @JoachimIsaksson: Read http://stackoverflow.com/a/40523/869736. There are important differences. – Louis Wasserman Apr 06 '13 at 18:16
  • @Joachim Isaksson As I remember in C++ there are "pointers" and "references", and you freely exchange them in one sentence. This is confusing, is not it? – Vitaly Apr 06 '13 at 18:19
  • @JoachimIsaksson In Java, you can't "repoint" references to another object (which is obviously what this question is about), I think that this actually works in C++. Moreover, C++ and Java are not really comparable in terms of passing as Java has `pass-by-value` and `pass-object-reference-by-value` whereas C++ has `pass-by-value`, `pass-by-reference` and `pass-by-pointer` . – Max Beikirch Apr 06 '13 at 18:23
  • @MaxBeikirch Apparently the correct term is [call by sharing](http://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sharing), which in Java is called pass by value (but, if the linked Wikipedia article is correct, in Ruby called pass by reference) – Joachim Isaksson Apr 06 '13 at 21:21
  • @JoachimIsaksson: Call by sharing is a nicer term indeed! – Max Beikirch Apr 07 '13 at 06:24

4 Answers4

2

Java uses pass-by-value but if the parameter is an object Java passes by value the reference to the object, so the called method can change the content of the object, not the object as a whole.

This does not mean that objects are passed by reference (the comment by Joachim Isaksson is wrong).

ADDED to answer to the comment by Arijeet Saha:

When I say "the called method can change the content of the object, not the object as a whole", I mean that if you change the object as a whole the caller doesn't see the change.

Consider the following example:

public void test(Person p) {
    p.setName("Pino");
    p = new Person();
    p.setName("John");
}

The first line of test() changes the content of the object received by the method, the second line changes the object as a whole (it assigns a new object to the formal parameter), the third line changes the content of the new object. In this case the caller sees a Person object with name "Pino", not "John", because the change made by the second line of test() is not visible to the caller; it is not visible because objects are not passed by reference.

Pino
  • 7,468
  • 6
  • 50
  • 69
  • what is meant by changing the content of the object but not changing the object as a whole? – Arijeet Saha Apr 06 '13 at 18:00
  • "but if the parameter is an object" The parameter cannot be an object. Objects are not values in Java. That is the point. – newacct Apr 07 '13 at 17:27
1

Let me first clear what does pass-by-value mean?

It means what ever you are passing to a method, it will recieve its copy not the actual adress.

So in your case you too are passing the value the variable a, and its value (which is referance to an object or adress to an object) is copied to the method(AB a).

Ahmad
  • 2,110
  • 5
  • 26
  • 36
0

Java's parameter passing is quite tricky - When an object is passed to a function, you can manipulate the object's fields but you cannot manipulate object itself. The object reference is passed by value. So, you can say:

class someClass{
  int i = 5;
}

class Foo
{
  static void func(someClass c)
  {
     c.i = 3;
  }
} 

class MainClass{
  public static void main(){
    someClass c = new someClass();
    System.out.println(c.i);
    Foo.func(c);
    System.out.println(c.i);
  }
}

Expect your output to be:

5
3

Changes to the fields of c persist.

but if manipulate the object itself, this manipulation will only persist in Foo.func() and not outside that function:

class someClass{
  int i = 5;
}

class Foo
{
  static void func(someClass c)
  {
     c.i = new someClass();
     c.i = 3;
     System.out.println(c.i);
  }
}

class MainClass{
  public static void main(){
    someClass c = new someClass();
    System.out.println(c.i);
    Foo.func(c);
    System.out.println(c.i);
  }
}

Expect your output to be:

5
3
5

What has happened? c.i has the value 5 in MainClass.main() in Foo.func(), c itself is modified to point to another instance of someClass, containing the value 3. However, this change is not reflected to the actual object that has been passed. The c in Foo.func() and MainClass.main() are different objects now. That's why changes to the c of Foo.func() do not affect the c in MainClass.main().

Max Beikirch
  • 2,053
  • 5
  • 25
  • 36
  • Why complicate things? There is nothing tricky here. Just simple definitions. – Vitaly Apr 06 '13 at 18:16
  • @Vitaly: I find it rather confusing that one can change the properties of an object in a function but not the object itself. I don't understand why `p.i = 3` and `p = new object()` should have different effects - although I understand why they do technically. Java is annoying :D – Max Beikirch Apr 06 '13 at 18:27
  • This adds simplicity actually, a caller always sure that callee will not change the object reference. Things are much more complicated in C++ where there are a lot of things around parameters. – Vitaly Apr 06 '13 at 18:48
  • It is unconsistent that `p.i = 3` changes the object whereas `p= new object()` doesn't. C++ has ways to specifiy what exactly you want. Do you want to change the object's fields but not the object itself? Pass it like `object* p const`. Do you want the object to be modifiable but not the object's contents? Pass `const object* p` instead. Do you want a copy? Pass it `object p` etc. If there is anything brilliant in C++, then it's passing! – Max Beikirch Apr 06 '13 at 19:46
  • It's consistent in the definition of a language. You just have other habits and preferences. A lot of people do not recommend to use pointers but recommend references instead in C++ which are mostly the same as Java references. That's safer. Is it correct? – Vitaly Apr 06 '13 at 19:54
  • If you pass a reference to a function, you can expect that you can still call functions on a reference. If you pass a pointer, the function may delete that pointer or make this pointer point to another object - and calling functions on a deleted object is quite hard ;) If one does not want that danger, he may pass the pointer like `pointer* p const`, making deleting or repointing the pointer impossible - pointers can be equally safe as references if one pays attention. I like that much about C++, although C++ is likely to do unexpected things... It's a always a tradeoff with every language :\ – Max Beikirch Apr 06 '13 at 20:20
  • "When an object is passed to a function" "Objects" are not values and cannot be passed – newacct Apr 07 '13 at 17:29
  • @newacct: So tell me how you would phrase it. – Max Beikirch Apr 07 '13 at 19:06
  • @MaxBeikirch: "Java parameters are always passed by value. The only values in Java are primitives and references. In the example below, `c` is a reference (pointer to an object). It is passed by value." – newacct Apr 07 '13 at 21:57
  • @newacct: Here you go: "The object reference is passed by value." (please read my answer completely before commenting.) – Max Beikirch Apr 08 '13 at 08:23
  • @MaxBeikirch: but the point is that there is nothing tricky at all. There is no difference in how things are passed regardless of type, once you realize that the only things in Java are primitives and references. People get confused precisely think they have a variable whose value is an object. Once they realize that they can't even have an object, then it becomes clear. – newacct Apr 08 '13 at 17:30
  • @newacct: I wasn't stating anything different in my answer. – Max Beikirch Apr 08 '13 at 19:28
0

Java always passes parameters as by value.

It's important to understand what is the value (what a variable holds).

For primitives it's the value itself.

For objects it's a reference.

When you pass an object as a parameter - the reference to the object is copied but it still points to the original object.

Vitaly
  • 2,760
  • 2
  • 19
  • 26
  • "When you pass an object as a parameter" The whole point is that "objects" are not values and cannot be "passed" – newacct Apr 07 '13 at 17:28
  • "When you pass an object as a parameter" - People usually say this when when write or read code like "function(object)". How do you think? – Vitaly Apr 08 '13 at 06:16
  • well it's important for people to understand that that's not possible -- it is not possible to have an expression "of object type" in Java. The only types are primitive types and reference types. So you can't possibly have "function(object)", only "function(reference)" – newacct Apr 08 '13 at 17:31