1

Ok so if primitive data types in java are passed into methods, they are treated as pass by value. And if object data types are passed into methods, they are treated as pass by reference right? So in this code:

//Class 1
public void passByValue(int x){
    x = 0;
}

public void passByReference(Integer y){
    y= 40;
}

//MainClass(contains main method)

    int primitiveType = 50;
    Integer wholeInteger = 100;
    Class1 a = new Class1();

    a.passByValue(primitiveType);
    a.passByReference(wholeInteger);

    System.out.println(primitiveType);
    System.out.println(wholeInteger);

This should result in primitiveType being equal to 50(variable hasnt changed). I understand that, however the Integer object is also not changed... So how does this work? Thanks!

user3843164
  • 369
  • 1
  • 4
  • 14
  • This question has been asked so many times... Java is "pass by value" and the reference to the object is also passed by value - so you don't change the reference that was sent! – Nir Alfasi Jul 19 '14 at 07:11
  • Check out my answer at http://stackoverflow.com/questions/24570360/parameter-passing-in-java/24570621#24570621 – EpicPandaForce Jul 19 '14 at 07:11
  • Okay then what about Collections.sort()? You don't have to do it like this: "listName = Collections.sort(listName);" Instead you can just do this: "Collections.sort(listName);" and listName will be sorted(i.e. changed). So it is PASS BY REFERENCE. – user3843164 Jul 19 '14 at 07:44
  • No. listName IS a reference. The reference is passed by value. – Christian Fries Jul 19 '14 at 07:58
  • So then how come my code example didn't work out..? I mean what's the difference between Collections.sort() and my methods? You just admitted that the reference is passed by value... so what does that even mean? – user3843164 Jul 19 '14 at 10:04

2 Answers2

1

Object references are passed by value too. It is the reference that is passed and not the object. You confuse pass-by-reference of the object with pass-by-value of the reference.

In A x = new A() the symbol x is a reference to an object of type A.

If x is a reference and f(A a) { a = null; } is a method then f(x) will not set x to null. Hence, the reference was passed by value.

The reason behind this (and your confusion) is that - to some extend - the phrase "the object is passed by..." is already wrong. Because correctly we have to say "the object reference is passed by...". If A is a class then A a; means that a is a reference to an object of class A. If that is clear, it becomes clear why we have a pass-by-value.

Christian Fries
  • 16,175
  • 10
  • 56
  • 67
  • Oh and since everyone is marking this question as duplicate, the reason why I asked it again even AFTER reading other so-called duplicates, I didn't understand the whole part about "passing the object pointer as value" which is why I asked it again, to get a clearer response. – user3843164 Jul 19 '14 at 07:46
  • Like that seriously doesn't make sense, why not just make java objects pass by reference? I mean what idiot made it so complicated? – user3843164 Jul 19 '14 at 07:47
  • I believe that the root of this is that in Java there are only references to objects (so A a in Java is A* a in C++). This is clear if you consider a field (member). The field is a reference to an object. That given we could pass that reference by value or by reference. Passing it by reference would be to pass a reference to a reference (which is even more complicated). – Christian Fries Jul 19 '14 at 07:55
0

First of all there is no pass by reference in java when you pass an object then it's also pass by value...

secondly, like String class , the wrapper class Integer is also immutable...

when you write Integer y= 40 ; then new Object new Integer(40); is being created by autoboxing feature java supports ..

That's why .... there is no change in value of wholeInteger you passed..

Anonymous
  • 67
  • 6