Results 1 to 5 of 5

Thread: What is difference between pass by value & pass by reference in java?

  1. #1
    Join Date
    Feb 2009
    Posts
    4

    What is difference between pass by value & pass by reference in java?

    Hi,

    I am a beginner in Java & i have some questions to ask you guys & learn things by communicating my questions with u?
    My question is,
    What is difference between pass by value & pass by reference in Java?

  2. #2
    Join Date
    May 2008
    Posts
    44

    Re: What is difference between pass by value & pass by reference in java?

    Java is strictly pass-by-value, exactly as in C. Read the JLS. It's spelled out, and it's correct. (See http://java.sun.com/docs/books/jls/s...doc.html#37472)

    In short: Java has pointers and is strictly pass-by-value. There's no funky rules. It's simple, clean, and clear. (Well, as clear as the evil C++-like syntax will allow

    In compiler terms, "pass by reference" means if you pass a variable into a method, its value can be modified. This is possible in many languages, like Pascal, Ada, and C++, but not in many other languages like C and Java. (Pascal had "var", Ada had "out", C++ had "&")

    For Java, take the case of

    Code:
    public void foo(Dog d) {
        d = new Dog("Fifi");
      }
    
      Dog aDog = new Dog("Max");
      foo(aDog);
    the variable passed in is not modified! After calling foo, aDog still points to the "Max" Dog!

    Many people mistakenly think/state that something like

    Code:
    public void foo(Dog d) { 
        d.setName("Fifi");
      }
    shows that Java does in fact pass objects by reference.

    The mistake they make is in the definition of

    Code:
    Dog d;
    itself. When you write

    Code:
    Dog d;
    you are defining a pointer to a Dog object, not a Dog object itself.

    Calling

    Code:
    foo(d);
    passes the value of d to foo. The value of a pointer is similar to a memory address.

    The use of the word "reference" in Java was an incredibly poor choice. Java has pointers, plain and simple. The designers of Java wanted to try to make a distinction between C/C++ pointers and Java pointers, so they picked another term. Under the covers, pointers are implemented very differently in Java and C/C++, and Java protects the pointer values, disallowing operations such as pointer arithmetic and invalid runtime casting.

    However, it makes no difference how pointers are implemented under the covers. You program with them exactly the same way in Java as you would in C or C++. The syntax is just slightly different.

    In Java,

    Code:
    Dog d;   // Java
    is exactly like C or C++'s

    Code:
    Dog *d;  // C++
    And using

    Code:
    d.setName("Fifi");  // Java
    is exactly like C++'s

    Code:
    d->setName("Fifi"); // C++
    To sum up: Java has pointers, and the value of the pointer is passed in. There's no way to actually pass an object itself as a parameter. You can only pass a pointer to an object.

  3. #3
    Join Date
    Apr 2008
    Posts
    1,948

    Re: What is difference between pass by value & pass by reference in java?

    Pass-by-value
    The actual parameter (or argument expression) is fully evaluated and the resulting value is copied into a location being used to hold the formal parameter's value during method/function execution. That location is typically a chunk of memory on the runtime stack for the application (which is how Java handles it), but other languages could choose parameter storage differently.
    Pass-by-reference
    The formal parameter merely acts as an alias for the actual parameter. Anytime the method/function uses the formal parameter (for reading or writing), it is actually using the actual parameter.

    http://javadude.com/articles/passbyvalue.htm

  4. #4
    Join Date
    Oct 2005
    Posts
    2,393

    Re: What is difference between pass by value & pass by reference in java?

    Not sure how familiar are you with C. But, people who know C or C++ can understand this in this way.

    If we are passing the value to the sub-routine, then it is called pass-by-value. The passed value is stored in another variable in called functions and operations are performed on that new variable. So, any changes made to the value (or variable) are not reflected back.

    If we are passing reference (pointer) rather value of the object, then it is called pass-by-reference. In this case, we are passing the reference to the object and all changes we make to the value it is holding will be reflected back automatically in program.

    In Pass by value copy of that variable sended to local variables of the function.
    In call by ref reference of that variable is sended.

    one more diff is in call by value the changes made to formal parameters will not affect actual parameters.

    in call by ref changes made to formal will affected by actual params

    in Java call by value is achieved by normal parameter passing and call by ref is achieved using passing of objects as parameters to functions.

  5. #5
    Join Date
    May 2008
    Posts
    115

    Re: What is difference between pass by value & pass by reference in java?

    Pass by Reference means the passing the address itself rather than passing the value and pass by value means passing a copy of the value as an argument.

    This is simple enough, however there is an important but simple principle at work here. If a variable is passed, the method receives a copy of the variable's value. The value of the original variable cannot be changed within the method. This seems reasonable because the method only has a copy of the value; it does not have access to the original variable. This process is called pass by value.

    However, if the variable passed is an object, then the effect is different. We often say things like, "this method returns an object ...", or "this method is passed an object as an argument ..." But this is not quite true, more precisely, we should say, something like "this method returns a reference to an object ..." or "this method is passed a reference to an object as an argument ..."

    Please have a look for further information about the differences

    http://www.javacertificate.net/passbyvalue.htm

    I hope this helps you!

Similar Threads

  1. Replies: 9
    Last Post: 30-08-2010, 04:57 PM
  2. Pass Variables By Reference in Javascript?
    By MAGAR in forum Software Development
    Replies: 5
    Last Post: 11-03-2010, 04:11 PM
  3. Pass Parameters To A Popup in Java
    By Adrina_g in forum Software Development
    Replies: 4
    Last Post: 23-02-2010, 09:01 PM
  4. How to pass reference to vector objects
    By Recko in forum Windows Software
    Replies: 3
    Last Post: 07-08-2009, 10:23 AM
  5. How to pass runtime arguments to JAVA Application
    By Nihar Khan in forum Software Development
    Replies: 2
    Last Post: 30-03-2009, 01:37 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,528,027.17721 seconds with 17 queries