-1

So I was experimenting with a permutation algorithm a few days back and discovered something.

int y=5;
chomp(y);
System.out.println(y); Output is still 5. Obviously.

void chomp(int x){
y=y-1;
}

The problem starts here.

char[] a = {'a','b','c'};
chomp(a);
System.out.println(a);

void chomp(char[] a){
char temp = a[1];
a[1]=a[2];
a[2]=temp;
}// It swapped it, But I didnt return anything. And I didnt do "a = chomp(a);"

BUT MY OUTPUT IS acb. WHY??? I tried it with int and nothing affected, From my experience in c and c++ im thinking because char array gives the address or something. But there is no pointers in java right? So how can it be???

OlivierLi
  • 2,798
  • 1
  • 23
  • 30
Luigi
  • 439
  • 5
  • 23

2 Answers2

2

in java, arrays are reference types, so only their references are copied. Reference types behave like pointers.

ints are value types, so their values are copied.

try a = {'x','y','z'}; in the chomp function. It won't change anything, because you're not changing the value that was at a, but a itself.

  • I was so confused cause they all say there is no pointers in java. And I noticed it behaved like a pointer. – Luigi Feb 17 '14 at 17:19
  • References are passed by value for objects, including arrays. – duffymo Feb 17 '14 at 17:19
  • Sam, Thank you very much. I tried it. It's supposed to be a = new char[] {'x','y','z'}; I got it, I've dealt with pointers deeply before, I got it!!!! It doesn't change. Thanks – Luigi Feb 17 '14 at 17:26
2

Java is pass by value - always. Primitives and references are the things that are passed.

Both your examples are correct, of course.

The array example is able to do the swap because you did not change the reference that points to the array. You were able to chance its state, as you are free to do with any mutable object.

It's imporant to know, because objects live on the heap. You don't pass an object to a method; you pass a reference to the object out on the heap. You can't modify the reference, but you can modify the state of the object it points to if it's mutable.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • I don't understand your last paragraph. You mean I have a choice to 'not pass' an object's reference to a method? How can I pass a char[] to a method without giving it reference? BTW Just now I did a char b = a.clone(); Worked well. – Luigi Feb 17 '14 at 17:35
  • 1
    Java is actually Pass-By-Value-Reference.. If it was actually pass by value, you won't be able to modify the original object using getters or setters as in C++. In Java, a copy of a pointer/reference to the object is passed: http://www.javaworld.com/article/2077424/learn-java/does-java-pass-by-reference-or-pass-by-value.html and http://stackoverflow.com/questions/40480/is-java-pass-by-reference Describing Java's object passing semantics really depends on your definition of reference.. – Brandon Feb 17 '14 at 17:42
  • *No*, read the link I gave. James Gosling quote: "There is exactly one ParameterPassing mode in Java--pass by value--and that helps keep things simple." – duffymo Feb 17 '14 at 17:47
  • Whoever downvoted this - you're incorrect. Explain your understanding to James Gosling, because that's where I got mine. – duffymo Feb 17 '14 at 17:48