aspose file tools*
The moose likes Beginning Java and the fly likes java is pass by reference or pass by value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java is pass by reference or pass by value" Watch "java is pass by reference or pass by value" New topic
Author

java is pass by reference or pass by value

Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 417
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?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18108
    
  39

http://faq.javaranch.com/java/CallByReferenceVsCallByValue


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Inder Kumar Rathore
Ranch Hand

Joined: Apr 14, 2010
Posts: 41
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......


Thanks

Living is easy with your eyes closed.....
Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 417
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

Joined: Apr 14, 2010
Posts: 41
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

Joined: Aug 16, 2005
Posts: 13868
    
  10

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java is pass by reference or pass by value
 
Similar Threads
Possible Memory Leak ?
Passed by reference or by value?
Passed By Reference?
If objects can't be passed....
Method parameters passed by reference/value