• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

it's about "Argument Passing"

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....
 
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
read this story !
 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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++)
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

+1 for good analogy
 
Nakul P. Patel
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it,thanks to all
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic