• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Pass by Value in JAVA

 
Ranch Hand
Posts: 79
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Marshal
Posts: 80487
455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic