Does Java Use Pass-By-Value Semantics?

Does Java Use Pass-By-Value Semantics?

if java passes objects by passing the reference variable instead its means the java uses pass-by-reference for objects, but its not a exactly.Java is actually pass-by-value for all variables running within a single VM. Pass-by-value means pass-by-variable-value. And that means, pass-by-copy-of-the-variable !


We are always passing a copy of the bits in the variable. So for a primitive variable, we are passing a copy of the bits representing the value.The bottom line on pass-by-value: the called method can\'t change the caller\'s
variable, although for object reference  variables, the called method can change the object the variable referred to.

For example, in the following code :

void bar() {
Foo f = new Foo();
doStuff(f);
}
void doStuff(Foo g) {
g = new Foo();
}

By:UMANG
Date:2027-02-09 00:00:00

Post Your Answers


User Email:

User Name:
Answers:

Related scjp1.5 chap3 Links

scjp1.5 chap3 interview questions and answers for experienced and fresher