Is pass by reference a gimmick in java? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Is pass by reference a gimmick in java?

I read in many forums that java is pass by value and not pass by reference(my java book stated that there is pass by reference in java)! Can u explain me in simple words why it isn't when we pass the reference of a object to another?

9th Jun 2019, 6:12 AM
Heisenberg
Heisenberg - avatar
1 Answer
0
Java is pass by value: One of the main reason is In java , because of oops, a object have its own separate copy of set of data variables. If we change one object variable, that doesn't affect other object data because of pass by value... If it pass by reference, then it happens to modified original variable values, Hence there all objects have same copy data, then what is need of object? A special case where we need single copy of variable to be available all objects as same value, then we have static for that purpose. We have to make variable as static. And one more important is that, in java arrays are passed by reference only. So we can say that pass by reference exist in java. I hope this gives you some understanding........
4th Nov 2019, 4:35 PM
Jayakrishna 🇮🇳