Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

it's about "Argument Passing"

 
Nakul P. Patel
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to java,preparing for exam.
I am confused in Argument Passing.My question is....
What is "Pass by Reference" and "Pass by Value"?

please give me suitable example....
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
read this story !
 
Javin Paul
Ranch Hand
Posts: 295
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In Short pass by value means you directly pass the value of argument while in case of pass by reference you pass the address where value is stored (usually passing pointers in C/C++)
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12097
30
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is sort of like the difference between mailing someone a word document and mailing them a url to a file.

pass by value makes a copy of the data and sends it over. if you make changes to what you get, it has no affect on the original.

pass by reference tells you where the file lives, so if you then go there and edit it, it is changed for everyone.
 
Javin Paul
Ranch Hand
Posts: 295
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

+1 for good analogy
 
Nakul P. Patel
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it,thanks to all
 
John Stark
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it is important that in Java there is only pass by value. If you pass a reference variable pointing to some object then it is still a copy of that variable which is passes to the method. If you make changes to that object by using the variable then the changes are also done outside the method:



But if you change the variable itself inside the method then this change is local to the method as you work with a copy:



In pass by reference you pass the address where the value of the variable is stored (which cannot be done in Java). If you pass a reference variable in Java then you pass a variable containing the address of some object. The variable is pass by value and you cannot change the stored address.

John
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic