Results 1 to 8 of 8
  1. #1
    sandeepsai39 is offline Member
    Join Date
    Feb 2009
    Posts
    96
    Rep Power
    0

    Default call by value and call by reference in java

    hi to all , i have some doubt with call by value and call by reference in java when a object is passed to a method call ,with proper method definition ,now what my doubt is it call by value[copy of the object] or call by reference.In some books it is call by reference ,in some other books it is call by value.In these two which is correct one .I belive that it is call by Value[copy of object].
    Thanks In advance and sorry for my language.

  2. #2
    Prajin's Avatar
    Prajin is offline Senior Member
    Join Date
    Jun 2010
    Location
    Ktm, Nepal
    Posts
    120
    Rep Power
    0

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by sandeepsai39 View Post
    hi to all , i have some doubt with call by value and call by reference in java when a object is passed to a method call ,with proper method definition ,now what my doubt is it call by value[copy of the object] or call by reference.In some books it is call by reference ,in some other books it is call by value.In these two which is correct one .I belive that it is call by Value[copy of object].
    Thanks In advance and sorry for my language.
    The books that say it's call by reference belong in the fire place; Java uses call by value, always. The obfuscating part is that Java doesn't pass objects around, it just passed pointers (references) around and it does so in a call by value manner, i.e. it copies the value of the reference and passes that around. Basically it is like C did it originally (nowadays C can pass copies or entire structs around). So never are there copies of objects made, that's why you can change the state of objects. If you want to copy entire objects you need a copy constructor or a clone method or another suitable method.

    kind regards,

    Jos

  4. #4
    shai is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default

    Primitive types such as int and double are passed by value, objects are passed by reference.

  5. #5
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Rubbish! OP, ignore shai's reply.

    Everything in Java is passed "by value". Everything.

    Pass-by-value
    - When an argument is passed to a function, the invoked function gets a copy of the original value.
    - The local variable inside the method declaration is not connected to the caller's argument; any changes made to the values of the local variables inside the body of the method will have no effect on the values of the arguments in the method call.
    - If the copied value in the local variable happens to be a reference (or "pointer") to an object, the variable can be used to modify the object to which the reference points.

    Some people will say incorrectly that objects are passed "by reference." In programming language design, the term pass by reference properly means that when an argument is passed to a function, the invoked function gets a reference to the original value, not a copy of its value. If the function modifies its parameter, the value in the calling code will be changed because the argument and parameter use the same slot in memory.... The Java programming language does not pass objects by reference; it passes object references by value. Because two copies of the same reference refer to the same actual object, changes made through one reference variable are visible through the other. There is exactly one parameter passing mode -- pass by value -- and that helps keep things simple.
    -- James Gosling, et al., The Java Programming Language, 4th Edition

    And
    JavaRanch Campfire - Pass By Value, Please
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  6. #6
    sandeepsai39 is offline Member
    Join Date
    Feb 2009
    Posts
    96
    Rep Power
    0

    Default

    thanks to all .I got the answer what i am accepting .Thanks to josah and phHein.


    The books that say it's call by reference belong in the fire place; Java uses call by value, always. The obfuscating part is that Java doesn't pass objects around, it just passed pointers (references) around and it does so in a call by value manner, i.e. it copies the value of the reference and passes that around.



    on small doubt which i don't know .
    JosaH used pointers word in his reply . Is java didn't remove pointers concept completly
    Last edited by sandeepsai39; 08-12-2010 at 10:40 AM.

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by sandeepsai39 View Post
    thanks to all .I got the answer what i am accepting .Thanks to josah and phHein.

    on small doubt which i don't know .
    JosaH used pointers word in his reply . Is java didn't remove pointers concept completly
    They're sort of pointers behind the scene; you can still see that in the NullPointerException, it should've been NullReferenceException. Java did remove all pointer arithmetic in order to guarantee type safety and help the garbage collector and some more but pointers are still under the hood and you can think of them as 'references', or 'cups' as the analogy goes in the JavaRanch article mentioned by PhHein; remember: call by value, always.

    kind regards,

    Jos

  8. #8
    sandeepsai39 is offline Member
    Join Date
    Feb 2009
    Posts
    96
    Rep Power
    0

Similar Threads

  1. System call with java 3
    By mike_ledis in forum New To Java
    Replies: 1
    Last Post: 03-26-2010, 06:51 PM
  2. How to Call a java service
    By omikeneil in forum Advanced Java
    Replies: 3
    Last Post: 10-17-2009, 03:30 AM
  3. how to call dll from java ??
    By Omarero in forum New To Java
    Replies: 3
    Last Post: 11-13-2008, 05:14 AM
  4. How to call C/C++ Program from Java
    By sanjeevtarar in forum Advanced Java
    Replies: 1
    Last Post: 04-17-2008, 08:55 AM
  5. how to call a dll from java
    By katie in forum Advanced Java
    Replies: 3
    Last Post: 12-10-2007, 10:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •