• 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

definition of call by reference and call by value

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear all,

I am a new bie to java.
i want to know the difference between call by reference and call by value definition.

Thanks & Regards.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In both the cases you will send the copies to the calling method as arguments. But the difference is when you pass the reference to method if that method changes the (primitive, reference)values of the reference then the original values wil get reflected, which is not the same in case of primitive values.
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Call by Value means that method arguments are the values of the original objects. This means a "copy" of the object will be given to the method. You cannot change the state of the original object from inside the method.
Call by Reference means, that methods arguments are just references to the original objects. This means you can change the state of the original object from inside the method.

Java does only provide call by value. Primitive values (e.g. int, long) are copied, so does references! The consequence is that you can change the object state from inside a method but you cannot change the reference itself (because its a copy of the original reference).
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch

Thank you for replying, both of you, but your replies were confusing, and I am not convinced you have understood pass-by-value and pass-by-reference.

This question has come up several times; look at this thread where I replied only a few minutes ago. Note the links to older threads and a JavaRanch FAQ. See whether any of those is helpful. There is one link in the old thread where it describes what happens in Pascal, which does support pass-by-reference.

The post again and see whether you have understood the problem.
 
reply
    Bookmark Topic Watch Topic
  • New Topic