Results 1 to 16 of 16
Like Tree2Likes
  • 1 Post By pbrockway2
  • 1 Post By quad64bit

Thread: java-object passing by value

  1. #1
    aumkar0406 is offline Member
    Join Date
    Dec 2011
    Posts
    10
    Rep Power
    0

    Default java-object passing by value

    Java only follow 'pass by value'..
    But not the pass by refference like in c,c++..
    In c,c++ we can pass value to other functions by pass by value and pass by address(rfference)..
    But in java only pass by value iz pozible..

    Any explanation abou it??a detail or a programme illustrating it..

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: java-object passing by value

    But in java only pass by value is possible.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: java-object passing by value

    Norm likes this.

  4. #4
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default Re: java-object passing by value

    Guys,

    I think pass by value and pass by reference , both possible in Java.

    Changes that occurred due to pass by reference will get reflected outside the method. But in case of pass by value changes wont reflected outside the loop.

    Please corect me If i am wrong.

    If u need any sample code, then please do let me know

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: java-object passing by value

    But in case of pass by value changes wont reflected outside the loop.

    Please corect me If i am wrong.
    When people speak of pass by value in Java they are referring to the fact that there will be no change to the value of a variable used as an argument that the caller can see.

    Java Code:
    int bar = 42;
    int foo = bar;
    someMethod(foo);
    System.out.println(foo == bar); // prints "true" because foo still has the same value
    
    Point bar2 = new Point(42, 666);
    Point foo2 = bar2;
    someOtherMethod(foo2);
    System.out.println(foo2 == bar2); // prints "true" because foo2 still has the same value.
    In both cases "true" will be printed. That's because the values of foo and foo2 do not change and cannot change whatever the methods are. If they were equal to bar and bar2 then they will remain so. Methods do not and cannot change the value of variables. That's because a copy of the value of the variable is passed to the method, and the method sees only this value. Java is pass by value.

    Of course, lots of other things might change when you call a method. Otherwise methods would be somewhat useless. But the value of variables won't change because the method is only passed a copy of this value.

    -----

    The links I posted above are well worth reading.

  6. #6
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: java-object passing by value

    I think pass by value and pass by reference , both possible in Java.
    Nope, pbrockway2 is correct. Some people might say java is pass by reference when dealing with objects, since you can 'pass' them to methods and it modifies the original, however, java is still passing by value.

  7. #7
    aumkar0406 is offline Member
    Join Date
    Dec 2011
    Posts
    10
    Rep Power
    0

    Default Re: java-object passing by value

    @quad64bit-----can we say java is both pass by value and pass by refference

  8. #8
    aumkar0406 is offline Member
    Join Date
    Dec 2011
    Posts
    10
    Rep Power
    0

    Default Re: java-object passing by value

    Plz post the sample code...

  9. #9
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: java-object passing by value

    @aumkar0406
    can we say java is both pass by value and pass by refference
    Nope! because it isn't. It is purely pass by value.

    Java is Pass-by-Value, Dammit! - Scott Stanchfield
    Fubarable likes this.

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

    Default Re: java-object passing by value

    This topic shows up again and again, doesn't it?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default Re: java-object passing by value

    Quote Originally Posted by JosAH View Post
    This topic shows up again and again, doesn't it?

    kind regards,

    Jos
    I'm getting flashbacks...

  12. #12
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: java-object passing by value

    Quote Originally Posted by pbrockway2 View Post
    Methods do not and cannot change the value of variables. That's because a copy of the value of the variable is passed to the method, and the method sees only this value.
    As far as I know variables are saved in the stack.
    Everything that is saved in the stack is copied when it passed as a parameter to a method.
    But objects(saved in the heap) are not copied and every edit that is done in the method will be vaild after that.
    Arrays for example, send them through a method, change a value and you will see that the value has changed after the method.

    If I didn't accurate in something correct me :)

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

    Default Re: java-object passing by value

    Quote Originally Posted by Tolls View Post
    I'm getting flashbacks...
    Maybe you have seen the broader perspective, but I haven't; I'm too low level; nuts and bolts so to speak ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: java-object passing by value

    I'm too low level; nuts and bolts
    That's a bit above stock and taps and dies.

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

    Default Re: java-object passing by value

    Quote Originally Posted by Norm View Post
    That's a bit above stock and taps and dies.
    Ha! So I made a promotion? ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  16. #16
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: java-object passing by value

    Quote Originally Posted by aumkar0406 View Post
    can we say java is both pass by value and pass by refference
    We can say anything we want. But if you say that you are wrong. Please read the pages linked to near the start of this thread to see why.

Similar Threads

  1. Error Passing Object through a constructor
    By mpankhurst in forum New To Java
    Replies: 9
    Last Post: 12-15-2011, 10:12 AM
  2. passing an object from one JDialog to Jframe
    By sinha in forum AWT / Swing
    Replies: 7
    Last Post: 12-16-2009, 04:56 PM
  3. Passing SWT object to another class
    By Vinaya Lal Shrestha in forum SWT / JFace
    Replies: 0
    Last Post: 04-03-2009, 01:50 PM
  4. passing object as value for checkbox values??
    By Pooja Deshpande in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 03-17-2009, 11:55 AM
  5. Passing a Vector object to a function
    By evapisces in forum New To Java
    Replies: 4
    Last Post: 09-27-2008, 03:18 AM

Tags for this Thread

Posting Permissions

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