I have read every where that primitive datatype and object references are passed by value?
I have tried searching in Google why doesn\'t java support pass by referen
My understanding is that the reason for not having pass-by-reference is mostly for security reasons: passing things by reference would enable a function to change stuff that is outside its scope and that means that my object (reference) may be replaced if I call a malicious function.
To elaborate: In Java, encapsulation is important and a safety measure. When giving a (pointer) to an object to a function that someone else wrote, I (as the caller) should be convinced that the function I call can only do with that object what I allowed it to do (using public members). Allowing a PBR would leave me (as the caller) in an unknown state after the function finished as I would not know if I am handling my own object or something else...
By design:
Some people will say incorrectly that objects are passed "by reference." In programming language design, the term pass by reference properly means that when an argument is passed to a function, the invoked function gets a reference to the original value, not a copy of its value. If the function modifies its parameter, the value in the calling code will be changed because the argument and parameter use the same slot in memory.... The Java programming language does not pass objects by reference; it passes object references by value. Because two copies of the same reference refer to the same actual object, changes made through one reference variable are visible through the other. There is exactly one parameter passing mode -- pass by value -- and that helps keep things simple. -- James Gosling, et al., The Java Programming Language, 4th Edition
As for deeper reasons, here's my take: it's the combination of two facts:
I can't help it if you don't like the first one. You'll have to tell James Gosling and Bill Joy and all the other folks who designed Java that they made a critical error. Good luck with that. Java is far more widely used than C++ today by several measures. The marketplace, however imperfect, has not penalized Java for what you perceive as an oversight.
Pass by value in C++ places burdens on both the developer (e.g. requirement of assignment and copy constructors) and the compiler writer (e.g. differentiating between stack and heap variables, all permutations of pass by value and reference with const and non-const).
The second one might have more of a technical explanation besides the designers' taste. I'm not an expert in the design and implementation of garbage collected systems, but perhaps that influenced their choice for a technical reason that I don't know.