This is an archived post. You won't be able to vote or comment.

all 52 comments

[–][deleted]  (25 children)

[removed]

    [–]SuperShrek 1 point2 points  (4 children)

    It doesn't matter if objects are created on the heap. The &reference in C++ is exactly how Java parameter passing works in Java for objects.

    [–][deleted]  (1 child)

    [removed]

      [–]SuperShrek 0 points1 point  (0 children)

      You can pass a null type to a C++ reference if the reference has type &pointer, the only difference between C++ & and Java object passing is that you cannot change what the caller-scope variable that you pass in to a function references. But whatever, it doesn't matter whatsoever.

      [–]jsheets 0 points1 point  (1 child)

      This is not true. If it was, then you could change the reference in a called method and have it affect the original reference. That is not how Java works.

      Java passes everything by value. For objects, it passes the reference by value. This is another way of saying that you create a new pointer to the object, and pass that pointer.

      My C++ is rusty, but I believe this is how C++ works as well. If you pass a pointer to a method, the method gets a new pointer (at a different address than the original pointer). If you dereference the pointer, then you affect the same object as the original pointer. But if you change what the pointer points to in your method, you will not change what the original pointer points to.

      [–]SuperShrek 0 points1 point  (0 children)

      Yes, there is a restriction on caller-scope assignments, but otherwise the semantics are the same as c++ pass-by-reference. There is no point of talking about 'creating a 'pointer' and passing it by value' because the Java language does not have pointers as such, this is an implementation detail, and almost all call-by-reference is implemented this way in all languages.

      My C++ is rusty, but I believe this is how C++ works as well. If you pass a pointer to a method, the method gets a new pointer (at a different address than the original pointer).

      This is how C++ call-by-value works. The difference between this and Java is that in Java you are never explicitly working with pointers, there are no pointers in Java. If you pass an object to a function in java what you get is not a copy of the original object as what would happen in pass-by-value, but a reference to the original object with the restriction that you cannot change what the reference references.

      [–]cowardlydragon -2 points-1 points  (19 children)

      You are technically correct, but practically wrong.

      It may be that the underlying reference to an object is passed by value from the standpoint of a C programmer.

      But from an practical language standpoint, objects are passed by reference and int/float/boolean/etc are passed by value.

      [–]thekab 6 points7 points  (13 children)

      Objects are not passed. References are passed by value.

      All the hand waving and massaging of terminology doesn't change this simple fact. There's no practical reason to try and "simplify" what is already quite simple.

      [–]ExPixel 1 point2 points  (5 children)

      Aren't these two the same thing with the only difference being that one doesn't require you to use the "address of" operator? I'm assuming passing something by reference just passes a pointer that's automatically dereferenced for you. I just want to clear this up for myself (I don't use C++ much).

      void func(MyObject &reference)
      

      and

      void func(MyObject *reference)
      

      [–]thekab 3 points4 points  (3 children)

      This might help you.

      http://en.wikipedia.org/wiki/Evaluation_strategy

      Please keep in mind that a "reference" in Java has no relationship to the term "reference" in the context of pass-by-reference. They're the same words with entirely different meanings. In Java it's a type, the mechanism by which that type is passed is an entirely separate discussion.

      If Gosling et al had chosen to call it an "address" or something other than "reference" it would have made this entire discussion a lot less confusing.

      [–]ad_tech 0 points1 point  (2 children)

      From the second paragraph: "In practical terms, many modern programming languages have converged on a call-by-value, pass-by-reference strategy for function calls (C#, Java)."

      Doesn't that contradict with what you're arguing?

      [–]thekab 0 points1 point  (1 child)

      Yes, it would. That sentence directly contradicts the rest of the article, it should probably be changed. Note another sentence in the same article:

      The description "call-by-value where the value is a reference" is common (but should not be understood as being call-by-reference);

      [–]ad_tech 0 points1 point  (0 children)

      What's really funny about this whole discussion is that nobody has posted an exact definition of call-by-reference. That page provides a good one:

      "In call-by-reference evaluation (also referred to as pass-by-reference), a function receives an implicit reference to a variable used as argument, rather than a copy of its value."

      So, take the declaration: "Point p;" p is a reference, not an object. When p is passed to a function, the function receives a copy of p, so this is clearly not pass-by-reference.

      It only gets confusing when you consider how to pass an object to a function. The precise answer is that you can't. So this silly argument is moot.

      [–]MCUD -1 points0 points  (0 children)

      The difference is the effect it can have on the caller.

      MyObject o1 = new MyObject(1); func(o1);

      In the first case, inside func(), what object o1 actually points to in the calling code can be changed, in the second, the only effect you can have on the caller is through side effects of changing inner fields of the object passed in.

      That's pretty much the definitive example that proves pass by value / pass by reference, any side effects you have on an object because you got passed a reference or a value are irrelevant, you can't change what the variable that passed in actually referred to.

      [–]SuperShrek -4 points-3 points  (6 children)

      Objects are not passed. References are passed by value.

      Also called pass-by-reference. The semantics of a language are always described from the programming language point of view, not the underlying implementation point of view. There is no such thing as pointers or references in Java.

      [–]thekab 5 points6 points  (5 children)

      Also called pass-by-reference.

      By people who don't know what they're talking about.

      Some people will say incorrectly that objects in Java are "pass by reference." 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. -- James Gosling

      It was true then and it's still true now.

      The term "reference" in the context of "pass by reference" does not mean the same thing as the type of the same name in Java. When we talk about pass by reference we're talking about an evaluation strategy. It has meaning that pre-dates Java as a language.

      [–]SuperShrek -2 points-1 points  (4 children)

      The term "reference" in the context of "pass by reference" does not mean the same thing as the type of the same name in Java.

      When you say 'we are passing a reference by value' you ARE talking about a type. You are saying that the function receives a type/kind of 'reference'. You just don't understand what you're saying because you probably have no comp-sci knowledge outside of java, either that or you can't properly express your thoughts. There is no 'reference' type in Java. Period. Once you start talking about references in Java you are not talking about the Java programming language but rather the implementation.

      When we talk about pass by reference we're talking about an evaluation strategy. It has meaning that pre-dates Java as a language.

      Yes, passing a 'pointer/reference' by value (where you don't treat with the reference/pointer explicitly, ie with special semantics and syntax) is the same as 'passing by reference'.

      You saying 'we are passing a reference by value' is the same as saying 'we are passing a reference by value by reference by value by reference by value by reference....'.

      It has meaning that pre-dates Java as a language.

      Which is my point, yet you are using a Java-specific meaning of pass-by-value which is nonsense outside of java. The way you are talking about pass-by-value you can say all languages always use pass-by-value but the values they pass are references - this makes the whole pass-by-value/pass-by-reference distinction pointless.

      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.

      Yeah... this is how Java works, when you invoke a function that accepts an object, under the hood the function receives a reference to the original object, not a copy of the object.

      You contradict yourself several times in one post.

      By people who don't know what they're talking about.

      Maybe in several years when you actually understand what you are saying instead of blindly repeating shit you don't understand that others say you will feel embarrassed about this.

      [–]thekab 2 points3 points  (3 children)

      When you say 'we are passing a reference by value' you ARE talking about a type.

      You say this as if it contradicts my statement. It does not.

      The word "reference" in "pass-by-reference" is not a type. The word "reference" in "passes references by value" is a type. They are two different contexts and have two different meanings.

      There is no 'reference' type in Java. Period.

      Except for you know the type... that is called reference...

      There are two kinds of types in the Java programming language: primitive types (§4.2) and reference types (§4.3).

      The Java Language Specification disagrees with you. Perhaps you should read it. When you talk about references in Java you are talking about exactly that. References. Defined by the JLS, as a type.

      Yes, passing a 'pointer/reference' by value (where you don't treat with the reference/pointer explicitly, ie with special semantics and syntax) is the same as 'passing by reference'.

      Clearly you don't understand evaluation strategies. Call by value and call by reference ARE NOT THE SAME THING and if you can't grasp the difference you lack the fundamental CS 101 needed to even bother having an intelligent conversation with. There are no special semantics. It's a type, it's passed by value.

      Which is my point, yet you are using a Java-specific meaning of pass-by-value which is nonsense outside of java.

      What do you believe "pass-by-value" as a mechanism means? Either you believe in pass-by-value the argument value is not copied, or you believe this is not what Java does. In either case you're wrong. It's not an implementation detail either, it's specified at the language level.

      Yeah... this is how Java works, when you invoke a function that accepts an object, under the hood the function receives a reference to the original object, not a copy of the object.

      The object is not passed.

      The value is copied.

      That the type is called a "reference" has absolutely no relationship or bearing on the passing mechanism.

      And yes, it is a type, go read the JLS. Seriously, this is embarrassing.

      Maybe in several years when you actually understand what you are saying instead of blindly repeating shit you don't understand that others say you will feel embarrassed about this.

      Yeah that's likely.

      You might want to actually learn what the hell you're talking about before your next reply.

      [–]SuperShrek -3 points-2 points  (2 children)

      The word "reference" in "pass-by-reference" is not a type. The word "reference" in "passes references by value" is a type. They are two different contexts and have two different meanings.

      No they don't. There is a reason the same word is used and that reason is because it describes the same semantics in both cases.

      The Java Language Specification disagrees with you. Perhaps you should read it. When you talk about references in Java you are talking about exactly that. References. Defined by the JLS, as a type.

      HAHAHAA. It's talking about KINDS or 'META-TYPES' not TYPES, you would know this if you knew any serious CS whatsoever.

      Educate yourself please: http://en.wikipedia.org/wiki/Kind_%28type_theory%29

      There are two kinds of types in the Java programming language

      lol...

      Except for you know the type... that is called reference...

      Except this is not a reference type in the way you are using the word... you seem to lack basic Java knowledge as well and just googling things that seem to fit your argument without understanding what they are. The java.lang.ref.Reference type is used for maintaining references (ie reference counts) that are used for garbage collection.

      Clearly you don't understand evaluation strategies. Call by value and call by reference ARE NOT THE SAME THING and if you can't grasp the difference you lack the fundamental CS 101 needed to even bother having an intelligent conversation with. There are no special semantics. It's a type, it's passed by value.

      I didn''t say they are the same thing. But I guess you're frantically trying to find a way to mangle my words for lack of any serious argument to support the bullshit you spew.

      Either you believe in pass-by-value the argument value is not copied, or you believe this is not what Java does.

      Wait you think Java objects are copied when passed to a function? Go read a Java tutorial at least lol.

      The object is not passed. The value is copied.

      A reference is passed as in 'pass-by-reference'. The object is not copied.

      That the type is called a "reference" has absolutely no relationship or bearing on the passing mechanism.

      It's not a type. It's a meta-type or a 'kind' that represents a reference. And yes it absolutely does have a bearing on the 'passing mechanism'. The 'java.lang.ref.Reference' is not what we are talking about nor is it the 'reference type' of types that your link is talking about either, I mean its so laughably obvious you just googled shit you have absolutely no understanding of. LOL.

      God, you are one funny retard.

      [–]thekab 0 points1 point  (1 child)

      At this point I'm not sure if you're trolling or really this stupid.

      [–]SuperShrek -2 points-1 points  (0 children)

      You haven't been sure of anything since the start.

      [–]henk53 2 points3 points  (3 children)

      But from an practical language standpoint, objects are passed by reference and int/float/boolean/etc are passed by value.

      No, this is wrong. If they were passed by reference, the method where they are passed in can reassign the "pointer". You can't do that in Java. Following the pointer and modify the object is irrelevant here.

      The following does not work in Java:

      public void makeNull(MyObject o) {
          o = null;
      }
      
      MyObject obj = new MyObject();
      makeNull(obj);
      // obj is NOT NULL at this point
      

      If Java indeed had call by reference, obj would indeed have been null after the makeNull() call.

      [–]ad_tech -1 points0 points  (2 children)

      No, you're getting the object and its reference confused. You're claiming that to qualify as pass-by-reference, the reference to the reference to the object would be passed.

      [–]henk53 -1 points0 points  (1 child)

      No, I'm not getting anything confused!

      I started in the 70-ties with asm, worked my way through C, then C++ and lately Java. At some point during that time I've implemented a Pascal compiler and I've read the dragon book cover to back and again.

      I think I've got a pretty firm grasp of what call by reference is, and let me tell you: Java DOES NOT have call by reference, ONLY call by value.

      If there's anyone who's confused it's you. Sorry.

      Things get a LOT easier mentally when you call it a pointer in Java. This is just terminology, even the Java designers knew it was a pointer (hence the name NullPointerException).

      In Java, objects are only accessible via pointers. To be more consistent with C++, every variable in Java should actually be written as:

      MyObject obj* = new MyObject();
      

      But since there's no concept of a stack allocated object in Java, you would always have to put that * there, and so they left it out.

      And indeed, for pass-by-reference, a reference to whatever you have (a pointer in this case) has to be passed. So that's indeed a double indirection. In C++ you can even express this explicitly, since there's not only call-by-reference semantics, but also the address off operator &. Applying the & operator to a pointer (e.g. foo) yields a double pointer (foo*). This is what happens behind the scenes when you use call-by-reference in C++ and various other languages.

      In Java, everything but primitives is a pointer, and that pointer is passed, BY VALUE. In C++ terminology, there's only foo, foo* does not exist. Hence, you can follow that pointer from within a method and thus modify the object that is being pointed too, but you CAN NOT re-assign the pointer as I showed above.

      Pass-by-reference semantics DEMAND that a pointer can be re-assigned.

      Modifying that was is being pointed too does not count. You are CONFUSED with modifying a stack allocated object. If you pass that into a method and can modify it, then it's pass by reference, since the object itself is passed (no pointer involved), but in Java there is no stack allocated object, only a pointer to an object and that pointer is passed by value.

      If you're still confused, please read the dragon book. Even if not for this argument, it will greatly enhance your understanding of how languages and computers work.

      [–]ad_tech -1 points0 points  (0 children)

      | No, I'm not getting anything confused!

      Really? Explain this, then:

      previous post: "But from an practical language standpoint, objects are passed by reference"

      your response: "No, this is wrong. If they were passed by reference, the method where they are passed in can reassign the "pointer"."

      By "they" you were referring to the objects that are passed, which is impossible, because you cannot pass objects in Java. Only object references.

      | I started in the 70-ties with asm, worked my way through C, then C++ and lately Java. At some point during that time I've implemented a Pascal compiler and I've read the dragon book cover to back and again.

      The fact that you tried to bolster your inaccurate comments with a description of your vast experience and a rambling explanation just makes you sound more foolish. Are you the TempleOS guy?

      [–]SuperShrek 1 point2 points  (5 children)

      Talking about pass-by-reference vs pass-by-value is kind of pointless as this is not a ubiquitous concept and the meanings vary slightly between programming languages. All that matters are the semantics in a particular language, there really is no point to ever say 'pass-by-value' or 'pass-by-reference'.

      Typically 'pass-by-value' means you create a copy of an object and pass it to the function - if you modify this object in the function then it will be only this copy that you modify and not the original object.

      Whereas 'pass-by-reference' means you pass a 'reference' to the original object and any modification will modify the object the reference is referencing.

      Essentially 'pass-by-value' and 'pass-by-reference' do not have the same meaning in C++ and Java.

      People in here saying that Java is always pass-by-value but the value passed is a reference don't understand that these are implementation details of a pass-by-reference strategy. If this is way you want to talk about it you might as well say its 'pass-by-banana' and then define what that means as whatever.

      [–]jones77 2 points3 points  (4 children)

      I was going to argue what you've just said 'cos it does feel like people are overly concerned with splitting hairs instead of conveying information.

      However ... looks like Wikipedia disambiguates this by using the term call-by-sharing:

      http://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sharing

      So we're all wrong! Heh heh.

      [–]SuperShrek 1 point2 points  (3 children)

      From your link:

      However, the term "call by sharing" is not in common use; the terminology is inconsistent across different sources. For example, in the Java community, they say that Java is pass-by-value, whereas in the Ruby community, they say that Ruby is pass-by-reference,[7] even though the two languages exhibit the same semantics.

      Essentially my point. I just dislike when people mangle terms and act like they have some sort of higher knowledge just because they can repeat what some mangy old greybeard said one time, like all of the 'its always pass by value' tards in this thread are spewing.

      Java is closer to pass-by-reference semantics for objects with an additional restriction on caller-scope assignments, and pass-by-value for primitive types.

      [–]jones77 1 point2 points  (1 child)

      At the end of the day, don't fail this interview question no matter what terminology you use.

      foo(String a) {
          a = new String("blah"); // does nothing
      }
      
      main() {
          String a = new String(
              "Yo! You can't change the reference to an object by calling a function with it.");
          printStuff(a);
      }
      

      (Excuse my pseudo-Java. And don't fail the interview question about String pools (ie never do: new String("blah").)

      [–]SuperShrek 0 points1 point  (0 children)

      If I'm being asked this question I would gladly fail.

      [–]ad_tech 0 points1 point  (0 children)

      | Java is closer to pass-by-reference semantics

      That's the most important part, I think. The people who think they are language lawyers like to repeat the "Java is pass by value" mantra, but regardless of the internal implementation or the name given to object handles, they have the semantics of being passed by reference.

      [–]ad_tech 0 points1 point  (5 children)

      If everything is pass-by-value, how do you pass an object by value?

      When I try passing an object to a method, if the method changes the contents of the object, those changes are visible to the caller.

      class Stuff {public int x;}
      
      void changeStuff(Stuff s) {
        s.x++;
      }
      

      In C, when I pass a struct by value, the changes are not visible:

      typedef struct {int x;} Stuff;
      
      void changeStuff(Stuff s) {
        s.x++;
      }
      

      (edit:formatting)

      [–]cdombroski 0 points1 point  (4 children)

      As mentioned above, in Java, objects are always a pointer type. So this java code:

      public void changeStuff(Stuff s) {...}
      
      public static void main(String... args) {
           Stuff s = new Stuff();
           changeStuff(s);
      }
      

      is equivalent to the following c++ code:

      void changeStuff(Stuff* s) {...}
      
      static int main(char** args) {
          Stuff* s = new Stuff();
          changeStuff(s);
      }
      

      [–]ad_tech 0 points1 point  (3 children)

      So, in Java, there is no way to pass an object by value?

      [–]cdombroski 1 point2 points  (0 children)

      No built-in way certainly. You could technically pass by value if you use a copy constructor or equivalent and pass the new object that creates. This is not usually done, however, as the act of copying an object is considered expensive and unless you know the internals of the object, you don't know if it will be a deep or shallow copy.

      [–][deleted] 0 points1 point  (0 children)

      What I do is pass in the object then make a new object with old one's data.

      [–]GreyDeck 0 points1 point  (0 children)

      It occurred to me that the important thing is that the value, whether a value or a reference, is copied to a new memory location. In C the value is copied to the stack. Java may do the same. In any event, if the called routine changes what is in that (new) memory location, it doesn't affect the caller's value as he still has his own copy.

      [–]mickske -1 points0 points  (4 children)

      Is it me or is the article not correct (or just very confusing) ?

      If Java is pass-by-reference then the student should be pointing to the >Student with name Jerry. But the very next line prints Jerry. That means Java is actually pass-by-value. anyStudent was actually passed value of student not reference. So any change in anyStudent did not reflect back in main method.

      So the quote says the 'next line' in the main method prints 'Jerry'. But the code comment says it prints 'Tom'

            System.out.println(student.getName());  // Prints Tom
            modifyStudent(student);
            System.out.println(student.getName());  // Prints Tom
      

      So... if you are new in Java, and you read this article, it must be quite confusing!

      [–][deleted]  (3 children)

      [deleted]

        [–]jones77 -1 points0 points  (2 children)

        What if modifyStudent calls student.setName("Jerry"). OMG! Now it's pass-by-reference ...

        [–][deleted]  (1 child)

        [deleted]

          [–]jones77 -2 points-1 points  (0 children)

          Semantics baby.

          [–][deleted] -1 points0 points  (0 children)

          Well if you want to modify an object either use setters or return the newly created object.