Originally posted by Nischal Tanna:
there will be no such concept of Pass By Reference
I hate to give anyone bad news, but it's true:
Java has no concept of pass-by-reference. Everything is pass-by-value -- even when you're passing a reference.
The name reference has a value which you can think of conceptually as the address of the "Test2" String. When you execute swap(name), a copy of that value is placed onto the stack for the swap() method to access. When swap() changes that value, it's changing the copy of the reference, so it has no effect on the main() method's name reference.
It seems like Java has pass-by-reference because you can alter objects through references passed to methods. Yet this is no different than passing a pointer to a struct to a C function. In that function you can alter the struct's fields but you cannot alter the pointer itself. To do that, you'd have to pass a pointer to the pointer in C or change the argument to a pointer reference in C++.
Neither of these are possible in Java.