• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java is pass by reference or pass by value

 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope java is pass by value. But, still objects in java gets passed as reference.

myobj as written below is object of MyClass and in changeVal() method, i am changing state of this object and it is getting reflected in Main method.

changeVal(myobj);

It looks like objects are still getting passed by reference.

Can anybody please clarify this?
 
Inder Kumar Rathore
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In java every thing is pass by value......
As you are passing the reference of object.....you are not changing the value of reference....
e.g.

public static void main(String df[])
{
SomeObj o=new SomeObj();
functionChange(o)
}

functionChange(SomeObj p)
{
p=new SomeObj();
}


In this the reference o is still pointing to old object(created in main)......
so there is no effect on the value of reference o so it is not pass by reference......
 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for the explaination.

But, behavior of java's call by value concept looks same as the behavior of the C++'s call by reference.

I got the concept that in java, copy of reference is passed while in C++, address of object itself gets passed. But , what is the use of supporting pass by value instead of pass by reference in java.
 
Inder Kumar Rathore
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, both manipulates the object......
but in java pointer manipulation is implicitly done and we are not allowed to use pointer in java....


 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raj Kumar Bindal wrote:But, behavior of java's call by value concept looks same as the behavior of the C++'s call by reference.

Look at the following line of code, which could be C++ or Java:

If this is C++, it has a different meaning than if this is Java. In C++, variables represent objects directly. So, in C++, the line above would create a SomeClass object, and the variable 'something' directly represents that object. In Java, the variable 'something' would be a reference to a SomeClass object. The line above in Java does not create a new object - it only creates a new variable that's initialized to null (if it's a member variable - local variables aren't initialized to a default value).

Raj Kumar Bindal wrote:But , what is the use of supporting pass by value instead of pass by reference in java.

Pass-by-reference is not necessary in Java, because variables of non-primitive types are references themselves. There's no need to have a separate pass-by-reference mechanism if you can just pass references by value.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49808
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raj Kumar Bindal wrote: . . . behavior of java's call by value concept looks same as the behavior of the C++'s call by reference. . . .
As Jesper has explained, the two behaviours are very different. It is hazardous to think that Java is some sort of extension or upgrade of C++. It isn't. It is a completely different language which happens to use similar syntax.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic