Java is always call by value. Java passes the copy of bits present in the container for the variable. Thus, in case of primitives, it passes a copy of actual value of the primitive, because the bit holder for primitive contains its actual value. While in case of object reference variables, the bit pattern represents a way to point(or in java terms, refer) to the object on heap. Remember, a reference variable never contains the actual object. All object reference variable in a single JVM have same bit depth.
If Java used pass by reference, then setting s = "Bye" in method() would have the effect of changing the s in main(). It doesn't, because Java does not pass by reference, even when it's passing references.
Hi, Your code explains the same fact that java uses pass by value. When you pass an object reference variable, you pass a copy of the bits contained in that variable, which actually is a way to point to that object. And when you pass that reference to a method, the reference becomes local to the method and its scope is limited to method. So when you reassign it, the reference changes, that is, Object to which the variable refers, changes.
In java, String objects are given special treatement. They don't behave like other object because of their immutability. That is, String objects, once formed are immutable. In your example, this property of Strings doesn't let you alter the object. You can try it using a StrinBuffer.
Try something like this:
You will notice that, when in the called method, we change the reference, then there is no change in the value of original object. But as we pass the reference, we can always change the original object. This, though, is not true in case of Strings. Sid [ May 15, 2007: Message edited by: Sidd Kulk ]