Hi,
Actualy java is pass by reference or pass by reference.I Know it is pass by value,But HOW?and what are the logic behind all these.and why this qs all put? pls I want the the detail theroy with examples.
Thanks
manoj
Printable View
Hi,
Actualy java is pass by reference or pass by reference.I Know it is pass by value,But HOW?and what are the logic behind all these.and why this qs all put? pls I want the the detail theroy with examples.
Thanks
manoj
All objects are passed by reference.
All primitive data types are passed by value.
This is a weakness in java that is not in C++. For example, assuming there is a class called Asdf
What is happening here is that you are creating a new Asdf called a. Then your passing in a reference to this new Asdf, but it now has a new name "b". "b", in the function is set to point at a new Asdf. However, a is still pointing at the original Asdf. Therefore, this function does nothing.Code:int main()
{
Asdf a = new Asdf();
function1(a);
}
void function1(Asdf b)
{
b = new Asdf();
}
It can get a little confusing, some of the explanations out there are unfortunately not very clear about what they're referring to. The links below should help you out a bit.
http://www-128.ibm.com/developerwork...raxis/pr1.html
http://www-128.ibm.com/developerwork...val/index.html
http://javadude.com/articles/passbyvalue.htm
I wouldn't necessarily call it a weakness, more like a compromise, giving up a little bit of flexibility for some security.
I suppose it is a compromise. Pointer arithmetic is something I'm definetly not going to miss.
The language is inconsistent and counterintuitive, but one thing's for sure: Java is pass by value, in every case. If you answer otherwise on Sun's Programmer Cert. exam, you will be marked wrong.
A value is something stored in a variable (or more precisely, something stored in the variable's location on the stack).
The only types of values in Java are primitives and references. A primitive is a value with a literal representation. A reference is a value that points/refers to an object.
Whenever you assign a value to a variable or pass a value as an argument, you are actually copying the value into the target variables location in the stack. This is known as "assignment/passing by value".
An object is not a value. It lives on the heap, not in a variable's location on the stack. Furthermore, an object is never assigned or passed, only its reference is, and as discussed above, the reference is copied (passed by value).
Java in a Nutshell explains this in greater detail, if you want more insight. I hope this was helpful.
Whatever you name it, it's the same thing. :wave:
mmmh, kind of.. a new pointer is attached to the object and made available to the called method. what actually gets passed is a value of a pointer that is a reference to the object.Quote:
Originally Posted by Chris256
you ought to have written "In my antiquated opinion, this is a weakness in java"Quote:
This is a weakness in java
of course, my opinion that your opinion is antiquated, is just my opinion..
yes, but we dont do things like that any more. If you loaned your mp3 player to a friend to put some tunes on, would you expect him to give you back a whole new ipod, with the tunes on? what if your mum bought you your ipod and you were really sentimentally attached to it? you'd be upset..Quote:
Code:int main()
{
Asdf a = new Asdf();
function1(a);
}
void function1(Asdf b)
{
b = new Asdf();
}
what if what he gave you back wasnt even an ipod but some low spec $30 mp3 player?
this was how we did things one upon a time, but now we have more secure ways of programming.. we dont give object A to object B for object B to fiddle with as it likes.. now if there is any fiddling to be done, its done through accessor methods on object A. if object B does something to A that A cannot do, support or otherwise doesnt like, then the operation stops, not the entire program.
what's happening here is actually this:Quote:
What is happening here is that you are creating a new Asdf called a. Then your passing in a reference to this new Asdf, but it now has a new name "b". "b", in the function is set to point at a new Asdf. However, a is still pointing at the original Asdf. Therefore, this function does nothing.
//make objectA
(pointerA) ----> [objectA]
//function1(a); //actually in java we have METHODS, not functions
(pointerA) ----> [objectA] <---- (pointerB)
//b = new asdf();
(pointerA) ----> [objectA]         (pointerB) ----> [objectB]
so the function (method) does actually do something; it makes a new object and point B to it instead. unfortunately then, it goes out of scope and both pointer B and object B are dissolved.
how is this a weakness?
care to give an example?Quote:
Originally Posted by jcovarru