This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Pass by value & Pass by reference  RSS feed

 
harshali patil
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somebody explain me pass by value and pass by reference by giving one example? And how it works in memory?

Thankx
Harshali
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

Read this and then especially this. These fun little stories are the best way I know of to learn about these concepts.
 
sven studde
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neither of those articles will tell you what "pass-by-reference" is, so you are not going to be able to know the difference between that and "pass by value" after reading them. To understand the difference, you would have to have an intimate knowledge of how "pass-by-reference" works in C++.

Here are three examples:

1)

The method call is unable to change the value of num in main(). All authors would describe that process as pass-by-value.

2)

The method call is able to change o's member variable num. Most Java authors call that pass-by-value too. Some C++ authors call that pass-by-reference.

3) This demonstrates a trickier situation:

'a' remains unchanged after the method is called. The rule is: unless 'a' itself appears on the left side of an assignment statement, you cannot change the object it refers to.
[ October 19, 2006: Message edited by: sven studde ]
 
David McCombs
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pass by reference is a concept that is independent of any language. You do not have to understand C++ to understand it. However, C++ is one of the major languages that supports it, but Pascal does and it is the only passing mechanism in Fortran. C does not, although arrays are passed by reference( I think), but it is not programmer controlled. Java does not have the ability to pass by reference either, but you can imitate the behavior of pass by reference. You can imitate it in C and ML as well. This can be done by passing mutable objects. Try to pass a immutable object or primitive type to a method and change it without returning it, then check its value the calling method.

References to objects in Java are implicitly pointers, and the value of the pointer is passed, not the object or the reference.

In pass by reference, the location of the variable is passed, not the value of the reference.

Lets say a Java reference is at address 0x05, for simplicity, and its value is 0xF3 and is the address of some object. If you pass this reference, the value 0xF3 is passed. For primitives, the actual value is passed, since that is what is being stored.

Lets assume for a minute that Java is pass by reference. So when a reference is passed, 0x05 is what is passed and the new reference is called an alias.

I hope this helps. I am not an expert on Java, so if I am wrong please correct me.
[ October 19, 2006: Message edited by: David McCombs ]
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arrays in C are passed by value, just as they would be in Java. It's just an illusion that thye are passed by reference, because an array is nothing more than a pointer to a memory location. Thus, the pointer (array) is passed by value.
 
David McCombs
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the correction.
 
sven studde
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
an array is nothing more than a pointer to a memory location.

Technically, that is incorrect. A pointer is a separate variable that has its own address in memory and stored at that address is the address of something else. Therefore, there are two addresses involved with a pointer. With an array, there is only one address involved. An array acts like a pointer in most situations, but it is not a pointer.


Now, it's your turn. Feel free to take a shot at this statement:

References in C++ are passed by value, just like in Java. It's just an illusion that they are passed by reference because references in C++ are implemented as constant pointers. Thus the pointer(a reference) is passed-by-value.
[ October 19, 2006: Message edited by: sven studde ]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!