Yes, exactly. Note that arrays are object types in Java.
No, not exactly. As others have already pointed out, Java has only pass
by value. Also mentioned, but perhaps not quite as clear, is that Java
doesn't pass objects at all, only references to objects (by value).
Indeed, in Java you can *never* put your figurative hands directly on an
Object: you can only manipulate objects via references. We are
typically sloppy in our terminology on this point: we say we have a
variable of type Foo or an array of Bar, when what we really have is a
variable of type *reference to Foo* or an array of *references to Bars*.
Similarly, we often say that we pass a Baz, when we really mean that
we pass a reference to a Baz -- by value.
So what's the difference between passing an object by reference and
passing a reference to it by value? It is a subtle distinction. The
key difference is what happens if you assign to the parameter in which
the argument was passed. Consider:
void doSomething(StringBuffer sb) {
sb.append("How many roads must a man walk down?");
sb = new StringBuffer("So long, and thanks for all the "); //*
sb.append("fish");
}
void doSomethingElse() {
StringBuffer buf = new StringBuffer("");
doSomething(buf);
System.out.println(buf);
}
When doSomethingElse() is invoked, what do you expect it to print?
With pass by value, at the line marked (//*) above, all that happens is
a reference to the new StringBuffer is assigned to the local variable
"sb", which happens to have been initialized with the StringBuffer
reference passed by the caller. This is quite invisible to the caller.
Since the buffer was previously appended to via the reference passed
by the caller, on return the caller sees the first append reflected via
its copy of the reference, and prints "How many roads must a man walk
down?". This is indeed what happens.
With pass by reference, however, the marked assignment would change the
value of buf in doSomethingElse(), so that the original StringBuffer is
lost. The second append would be visible, so the method would print
"So long, and thanks for all the fish". This is not what happens.