Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 50
Like Tree2Likes

Thread: Getting more than one "return" from a method

  1. #21
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: Getting more than one "return" from a method

    Quote Originally Posted by Tolls View Post
    Actually, java uses pass by value (that's the term I think you're looking for).
    Yes, in relation to objects it is a reference that is passed, it is still only the value, not the actual reference itself, which is why the following has no effect on the reference passed in:
    Java Code:
    void someMethod(SomeClass sc) {
        sc = new SomeClass();
    }
    
    void someOtherMethod() {
        SomeClass sc = new SomeClass();
        someMethod(sc);
        // HERE.
    }
    At the point HERE the sc reference in someOtherMethod() still refers to the original object, not the one created in someMethod(). If Java were pass by reference then it would refer to the one created in someMethod().
    No. Java passes by reference.

    C++ passes by copy (or value): different terms for the same thing.

    In Java, the value of the reference is passed. So the method receives a copy of the reference (which explains your example).

    BUT IT IS STILL CALLED PASS BY REFERENCE. IT IS IMPORTANT TO GET THIS RIGHT.

    In C++, the method receives a copy of the object on the heap (unless you use the & to denote pass by reference).

    The example you have given would not affect the sc in someMethod(), irrespective of whether you are passing by reference or copy/value. In fact the syntax in C++ wouldn't even allow you to do it.
    Last edited by 2by4; 12-16-2011 at 10:57 AM.

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

    Default Re: Getting more than one "return" from a method

    In the bag of oranges illustration a critical observation (in bold), and what makes the return of anything unnecessary, was that the shopkeeper gets a reference but the shopper keeps their own. And that sounds like a copy is being made.

    Another way of seeing the same thing is to note that whenever a variable is used as an argument in a method invocation that variable will have the same value after the method invocation as it did before. There is nothing whatsoever that a method can do which will change the value of a variable in the caller's context. (Mind the difference between changing a variable's value and changing the state of the thing referenced by that value.) Since the caller's variable is absolutely immune to change brought about by the method it, again, seems reasonable to speak of the method receiving a copy of the variable's value.

    (The preceeding paragraph applies with equal force to C, but not to C++ which allows both method calls which can and those which cannot affect the value of the variable used as an argument.)

    In general it may be an expression not a variable that is used as an argument. Either way "When the method or constructor is invoked (15.12), the values of the actual argument expressions initialize newly created parameter variables". (JLS 8.4.1) Classic pass by value.

  3. #23
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: Getting more than one "return" from a method

    In the terminology "pass by reference or by value", reference and value are qualifying the object.

    That is where the confusion is coming in. If we are talking about references, they are always copied. That is tangential to the what the terminology is describing.

    The point is that, in Java there is only one Object. The method creates a new reference INITIALIZED to the SAME Object. The new reference can subsequently point away from that one Object but that, in itself, doesn't change the Object nor the reference that was passed in. The method can change the Object's fields and invoke its methods, but no new copies of Object are automatically created at language level.

    So, I repeat: JAVA PASSES BY REFERENCE.

    C, and C++ are different. They pass by value (i.e copy). A duplicate object is created at that point.

    (In C++, you can use the & operator to get the same behaviour as in Java.)

    C and C++ PASS BY VALUE.

    You have to think of the object, and not confuse it with any references to it, which are always copied into methods -- and that copying of value has nothing to do with the terminology, "copy by value" which refers to the object.

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

    Default Re: Getting more than one "return" from a method

    Quote Originally Posted by 2by4 View Post
    So, I repeat: JAVA PASSES BY REFERENCE.
    And I repeat: bollocks; Java passes parameters by valuel; always.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #25
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: Getting more than one "return" from a method

    Quote Originally Posted by JosAH View Post
    And I repeat: bollocks; Java passes parameters by valuel; always.

    kind regards,

    Jos
    Read it again, carefully. Then you'll understand.

    Java does indeed pass parameters by value, always, but the terminology is not about parameters.

    "Pass by" refers to objects..how does the method or function access the object.

    In Java, the method accesses the original Object through a reference to that object.

    So, I repeat, once again, JAVA IS PASS BY REFERENCE!

    In C++ functions access objects by copy -- they access the values only, of an object through a copy of that object.

    So, I repeat, once again, C++ IS PASS BY VALUE!

    Don't confuse it with what is happening to the parameter, as I said earlier.
    Last edited by 2by4; 12-16-2011 at 01:22 PM.

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

    Default Re: Getting more than one "return" from a method

    Please get your facts straight before you start shouting.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #27
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: Getting more than one "return" from a method

    Quote Originally Posted by JosAH View Post
    Please get your facts straight before you start shouting.

    Jos
    I highlighted key terms. That is not shouting. You are the one swearing aren't you.

    On the actual issue, look at it this way..ask yourself...

    in Java does the method know about the objects reference? Does it know where the object is on the heap?

    ANSWER: Yes, because Java passes by reference.

    Does a C++ function know where the object is on the heap?

    ANSWER NO: It receives a copy and cannot know where the original object is. It only knows the original object's values. C++ passes by value.

    (C++ can be made to work like java by using the & symbol for, guess what,.... "reference types" ;-) )

    There is plenty of literature around with people getting it wrong, because they are looking at parameters.

    It is a subtle point. Passing what? Passing information. What information? The object information, not the fiddly low level stuff.

    So to clarify:

    Java passes object information by referring to the object.

    C++ passes object information by copying it's actual value.
    Last edited by 2by4; 12-16-2011 at 01:50 PM.

  8. #28
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Getting more than one "return" from a method

    Java is pass by value.
    There are no two ways about it.
    If it were pass by reference the code I posted aboove would result in the outer sc being changed.

    If you were to answer the question "Is Java pass by reference or pass by value" with "pass by value" you would fail that interview question.

  9. #29
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: Getting more than one "return" from a method

    In an interview I would simply explain the mechanism and mention that there is controversy over the terminology.

    It boils down to the question, "what aspect of what is being passed are we talking about" We all know the effects; we are just not agreeing on terminology.

    I understand your position, and the semantics you are using.

  10. #30
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Getting more than one "return" from a method

    There is no controversy over the terminology.
    Honestly.

    No one who knows how Java works would answer "pass by reference". It may pass a reference, but that reference is passed by value. It is copied.

    From the horses mouth, "Passing Reference Data Type Arguments" section:
    "
    Reference data type parameters, such as objects, are also passed into methods by value. This means that when the method returns, the passed-in reference still references the same object as before. However, the values of the object's fields can be changed in the method, if they have the proper access level.
    "

  11. #31
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: Getting more than one "return" from a method

    There is no question there, because they have carefully specified that they are talking about arguments. Other people talk about objects, which I find more natural.

    Don't be aware of the controversy if you don't want to. If you do want to, just Google "pass by reference" and see the ranting and swearing that is going on. You may even find this thread :-)

  12. #32
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: Getting more than one "return" from a method

    Well, I have certainly soften my stance on this. Thank you everyone. I will certainly not be saying one alternative or the other is wrong any more.

    What is of primary importance is understanding how code will behave, but it is certainly clearer in my head what the semantic differences are, and how the terminology can be used.

    When the term is used, I think it would be wise to ask, "what do you mean?", rather than assume. That is what I have learned.

  13. #33
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: Getting more than one "return" from a method

    Here's an OO guy talking about pass by reference/copy

    http://processing.org/learning/objects/

    When a primitive value (integer, float, etc.) is passed in a function, a copy is made. With objects, this is not the case, and the result is a bit more intuitive. If changes are made to an object after it is passed into a function, those changes will affect that object used anywhere else throughout the sketch. This is known as pass by reference since instead of a copy, a reference to the actual object itself is passed into the function.
    I can identify with what he is saying.

    I'm beginning to recognise two schools of thought, and those who think more in high level OO terms are using different terminology from those thinking in technical, nuts and bolts terms.

  14. #34
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: Getting more than one "return" from a method

    Another OO guy seeing things in terms of objects and not parameters...

    Aggregation

    Aggregation, as a relationship, is reliant on the ability to pass object and function parameters by reference. This basically means that a parameter represents a pointer to the actual object passed and not just a copy. Thankfully, Javascript handles all parameters in this way which makes our aggregation relationship nice and easy:
    Object-Oriented Javascript - Nefarious Designs

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

    Default Re: Getting more than one "return" from a method

    My understanding is that Javascript is also pass by value (see 11.2.3 and 11.2.4 of ECMA-262): the arguments are evaluated and those values are passed to the relevent [[Call]] internal method. I don't know about the Processing language.

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

    Default Re: Getting more than one "return" from a method

    Quote Originally Posted by 2by4 View Post
    Here's an OO guy talking about pass by reference/copy

    http://processing.org/learning/objects/



    I can identify with what he is saying.

    I'm beginning to recognise two schools of thought, and those who think more in high level OO terms are using different terminology from those thinking in technical, nuts and bolts terms.
    I don't know if you consider this 'nuts and bolts' but a nice litmus test for the pass by value mechanism is the following: (pseudo code)

    Java Code:
    T a, b;
    
    swap(a, b);
    If you can write the swap method (it has to swap objects a and b obviously, not just their contents) in the particular language, the language supports some other parameter passing mechanism besides 'pass by value'. In Java you can't wite such a method; it has pass by value only. In C++ such a swap method can be written; C++ has (also) pass by reference.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  17. #37
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: Getting more than one "return" from a method

    Quote Originally Posted by pbrockway2 View Post
    My understanding is that Javascript is also pass by value (see 11.2.3 and 11.2.4 of ECMA-262): the arguments are evaluated and those values are passed to the relevent [[Call]] internal method. I don't know about the Processing language.
    It is not the mechanism that is in dispute. It is the terminology. We know the behaviours and outcomes, but different people are talking about different aspects of what is being passed. The OO lobby think in terms of data flow and object properties being passed. The technical details of the vehicle for passing that information is of no major concern to them. I totally understand why you would say JavaScript is pass by value.

  18. #38
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: Getting more than one "return" from a method

    Quote Originally Posted by JosAH View Post
    I don't know if you consider this 'nuts and bolts' but a nice litmus test for the pass by value mechanism is the following: (pseudo code)

    Java Code:
    T a, b;
    
    swap(a, b);
    If you can write the swap method (it has to swap objects a and b obviously, not just their contents) in the particular language, the language supports some other parameter passing mechanism besides 'pass by value'. In Java you can't wite such a method; it has pass by value only. In C++ such a swap method can be written; C++ has (also) pass by reference.

    kind regards,

    Jos
    It is nuts and bolts, and I'll tell you why. You are thinking low level, in terms of memory locations.

    The OO guy would think in terms of what information is encapsulated in a and b. Of course this can be swapped using Java. The OO guy doesn't care whether that information is has changed it's physical location on a physical computer.

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

    Default Re: Getting more than one "return" from a method

    It is not the mechanism that is in dispute. It is the terminology. We know the behaviours and outcomes, but different people are talking about different aspects of what is being passed. The OO lobby think in terms of data flow and object properties being passed. The technical details of the vehicle for passing that information is of no major concern to them. I totally understand why you would say JavaScript is pass by value.
    But information about objects is not being passed (much less the objects themselves); what is being passed is a way of getting that information. The two are no more alike than a key and the inside of a safe. You can put a key in your pocket!

    In Java that "way of getting" is a reference: more precisley, the value of a reference variable. A value that is copied and passed.

    -----

    Yes, it's just about terminology. I wouldn't start a holy war over it, but I think the "traditional" Java terminology has a couple of things going for it. First, as against those who describe the semantics as PBR, it doesn't efface a very real distinction between languages pointed to by Jos' swap example. Secondly it doesn't make an issue of the fact that object properties can be accessed despite the fact that values are being copied and passed. (called "object sharing" semantics decades ago) Rather it stresses what primitive and reference values have in common. I think Gosling's phrase was it "keeps things simple" that values (both sorts) are copied and passed, or copied and assigned.

  20. #40
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: Getting more than one "return" from a method

    Quote Originally Posted by pbrockway2 View Post
    But information about objects is not being passed (much less the objects themselves); what is being passed is a way of getting that information. The two are no more alike than a key and the inside of a safe. You can put a key in your pocket!

    In Java that "way of getting" is a reference: more precisley, the value of a reference variable. A value that is copied and passed.
    You can keep repeating what you prefer to think is being passed.

    I have moved on and accepted that different folk are focusing on different aspects.

    An OO person thinks in terms of objects and data flow, not technical vehicles.

    Quote Originally Posted by pbrockway2 View Post
    Yes, it's just about terminology. I wouldn't start a holy war over it, but I think the "traditional" Java terminology has a couple of things going for it. First, as against those who describe the semantics as PBR, it doesn't efface a very real distinction between languages pointed to by Jos' swap example. Secondly it doesn't make an issue of the fact that object properties can be accessed despite the fact that values are being copied and passed. (called "object sharing" semantics decades ago) Rather it stresses what primitive and reference values have in common. I think Gosling's phrase was it "keeps things simple" that values (both sorts) are copied and passed, or copied and assigned.
    Oh, yes, both aspects have something going for them. A tug-of-war over a piece of terminology is unnecessary. What is important is recognising usage, rather than remaining in ignorance of it.

    The swap example demonstrates nuts and bolts thinking. It is not OO. In OO terms it is not sensible to talk of swapping objects in "free space" so to speak. You can swap their attributes. But if you want to swap the objects themselves, it has to be done within a composite relationship of an enclosing object. Then, of course, it is the enclosing object that gets passed to swap(); this can be done in Java.

    (I should have been more general. What you are swapping is the relationship the objects have with another class. So it needn't be a composition.)
    Last edited by 2by4; 12-17-2011 at 12:02 PM.

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. Replies: 3
    Last Post: 10-12-2010, 04:21 PM
  2. Replies: 1
    Last Post: 01-21-2010, 09:20 AM
  3. Replies: 3
    Last Post: 05-25-2009, 08:50 PM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 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
  •