Hi to All, I have read on many books and websites that Java supports pass by value. I have also read that primitives are passed by value and Objects are passed by reference. Since Java is object based language, it should have supported passed by reference. Isn't it contradictory issue? I am very much confused:-( Do you have any words to remove my confusion??
They say in java everything is pass by value. Primitive is pass by value, meaning copy of the number or character or whatever. Original is unaffected.
When it comes to reference variable or pointer, they make a copy of that and passed into the function/method. Original reference variable/pointer is untouched. However even though it is a copy, it still points to the same object as the original pointer. Meaning it can call methods of that object that changes the object's fields/properties like the original pointer. The tricky thing is that you can never mess up the original pointer no matter what you do with the copied one.
Originally posted by Krishna Acharya: I have also read that primitives are passed by value and Objects are passed by reference.
Wherever you read that, it's wrong and confusing.
Everything is passed by value in Java. Variables of non-primitive types are really references to objects. If you pass such a variable to a method, the reference is passed by value. Note that that's not the same as passing by reference.