aspose file tools*
The moose likes Beginning Java and the fly likes Pass by Value in JAVA Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Pass by Value in JAVA" Watch "Pass by Value in JAVA" New topic
Author

Pass by Value in JAVA

adithya narayan
Ranch Hand

Joined: Jan 05, 2009
Posts: 79

Hi,

This is a very basic question i am asking.

1)In the following code i am having an instance reference variable initialized to a default value(10). What i thought was if i passed a copy reference variable to a method and modified the object to a different value(20), then the object should retain the value because the copy reference points to the same object on the heap and we can modify the Integer object using any reference as long as it points to it but it didn't happen so !!

I tried the same thing with a local object reference.

I passed the class objects reference variable () and then it worked but i would like to know why can't the instance/local object be modified by passing the reference of the object.

Can anyone please explain?

2)Where does the instance reference variable live ; on stack or heap ?




Thanks,
Adithya.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

adithya narayan wrote:
2)Where does the instance reference variable live ; on stack or heap ?

Heap ; by the way: I could not understand the purpose of your program
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

adithya narayan wrote:
i am having an instance reference variable initialized to a default value(10). What i thought was if i passed a copy reference variable to a method and modified the object to a different value(20), then the object should retain the value because the copy reference points to the same object on the heap and we can modify the Integer object using any reference as long as it points to it but it didn't happen so !!

because Integer is an immutable! when you change the Integer to 20, it is a new Object! . this is the exact scenario why working with immutable object is easy in multi threaded environment.

after first run see the result then, uncomment the Integer object also comment the StringBuffer object and see the output.


hope this helps!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14435
    
  23


Java always passes by value. What might be a little bit confusing is that Java also passes references by value. Note that passing a reference by value is not the same as pass by reference!

Let's carefully look at what your program does.

In line 3 you declare a member variable called integer in class PassByValue.

In line 7, in the constructor of class PassByValue, you assign a value to the member variable.

In line 12, you are declaring and initializing a local variable called integer. Note that that variable does not have anything to do with the member variable of line 3 - it's a completely separate variable, that just happens to have the same name.

In line 13, you are declaring and initializing a local variable called passByValue that is an instance of class PassByValue. That instance will contain an instance of the member variable, initialized to new Integer(10) by the constructor.

In line 16, you are printing the content of the member variable. It will print "10" as the value.

In line 18, you are calling the method on the instance of class PassByValue, and you pass it the local variable. Note that in the method, the argument copyInteger will now refer to the same Integer object that the local variable integer in line 12 refers to. The reference integer is copied to copyInteger.

In line 24, you make copyInteger refer to another value. (Note that in this line, you are using autoboxing). Note that making copyInteger refer to something else does not have any effect on the local variable integer in the main() method - copyInteger originally contained a copy of the reference, it doesn't have any link back to the variable integer in the main() method.

In line 19, you are printing the content of the local variable integer. It will print "15" as the value. The method call of line 18 didn't have any effect on the local variable integer.

Seetharaman also makes an important point: class Integer is immutable. The value of an Integer object cannot be changed after it has been created. In line 24 you are not changing the value of the Integer object; you're just making copyInteger refer to a different Integer object.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40061
    
  28
This discussion from 1½ years ago is about the same sort of question. Note it is a very long thread because some people found difficulty understanding the concept, but you should find some useful explanations there.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Pass by Value in JAVA