Manjunath Dyavannanavar wrote:Java support only pass by value. If you're sending an object ideally you will send the object reference, this reference will be copied to method parameter ( system.out.print(object) will give you a address of the object). So in this case object reference will be used throughout the function, that's why the changes in other methods will reflect in main method. If you reinitialise the passed object than the changes will not reflect in main method.
If you want to change primitive types than do wrapper class.
Sometimes the only way things ever got fixed is because people became uncomfortable.
Tim Holloway wrote:
No. Java does pass by reference for objects, and pass by value for primitives, including primitive expression results.
That is my understanding too: pass by value only. This JavaPapers link explains it; the section from the JLS (=Java® Language Specification) tells you that the method uses newly created parameter variables.Dave Tolls wrote:. . . You can't change the valueof a parameter passed into a method and have it reflected outside of that method.
Campbell Ritchie wrote:What confuses people is that mutable reference types can change their state inside a method and they don't know that isn't pass by reference.
Please explain more. Wrapper objects are usually immutable. I don't see how you can use them to change primitives.Manjunath Dyavannanavar wrote:. . . If you want to change primitive types than do wrapper class.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |