12 Replies Latest reply: Jan 31, 2009 6:55 PM by 796440 RSS

    Java passes object references by value.

    807588
      Hi,

      I heard this statement in a lecture "Java passes object references by value" . Well this statement i can interpret when it comes to client server architecture but is there any principle of "object reference by value" in standalone application.

      Regards
      Viru.
        • 1. Re: Java passes object references by value.
          JoachimSauer
          virupatil wrote:
          I heard this statement in a lecture "Java passes object references by value" . Well this statement i can interpret when it comes to client server architecture but is there any principle of "object reference by value" in standalone application.
          I'm sorry to tell you, but if you can only think of what it means in a client-server architecture, then you're probably interpreting it wrong.

          Could you describe with your own words what a object reference is?
          • 2. Re: Java passes object references by value.
            807588
            Hi,

            Well object reference means a reference of the object on which the functions can be invoked instead of directly manipulating the object itself.
            • 3. Re: Java passes object references by value.
              796440
              virupatil wrote:
              Hi,

              I heard this statement in a lecture "Java passes object references by value" . Well this statement i can interpret when it comes to client server architecture but is there any principle of "object reference by value" in standalone application.

              Regards
              Viru.
              Passing semantics have NOTHING to do with "client server architecture". It's a feature of the language, and it applies regardless of whether you're developing a desktop app, an application, a library, a web app, or whatever.
              • 4. Re: Java passes object references by value.
                796440
                JoachimSauer wrote:
                virupatil wrote:
                I heard this statement in a lecture "Java passes object references by value" . Well this statement i can interpret when it comes to client server architecture but is there any principle of "object reference by value" in standalone application.
                I'm sorry to tell you, but if you can only think of what it means in a client-server architecture, then you're probably interpreting it wrong.
                I'd say if client-sever comes into it at all he's got it wrong.
                • 5. Re: Java passes object references by value.
                  807588
                  Ok,

                  But i just want to know what exactly does object reference mean?
                  • 6. Re: Java passes object references by value.
                    807588
                    virupatil wrote:
                    Hi,

                    Well object reference means a reference of the object on which the functions can be invoked instead of directly manipulating the object itself.
                    You never manipulate objects directly. It always goes through references.

                    I guess by "reference" you're thinking about some kind of virtual reference over a network, which is why you brought up client/server, but that's not what "reference" means in this context at all.

                    Consider this code:
                    String s = "hello world";
                    s is a variable whose type is a reference to a String object, and whose value is the particular reference to that particular String.

                    People might say "s is a string" but that's just shorthand for the above.

                    When you pass s to a method:
                    doSomething(s);
                    you're passing the value of s as an argument, matching a parameter of doSomething whose type accepts a reference to a string (for example, the type of the parameter could be a reference to an Object or a reference to a CharSequence, or simply a reference to a String).
                    The corresponding parameter is a completely different local variable in doSomething, and its value will be the value of s -- that is, a reference to a single object.
                    • 7. Re: Java passes object references by value.
                      JoachimSauer
                      virupatil wrote:
                      Ok,

                      But i just want to know what exactly does object reference mean?
                      An object reference is something which referes to (i.e. points to) an object.

                      In Java, all variables that aren't of a primitive type (int/short/...) are reference variables. That means they hold object references (note, that a variable never holds an object in Java! A variable can only hold a primitive value or a reference to an object or the null reference).
                      • 8. Re: Java passes object references by value.
                        807588
                        Thanks everyone I got it . :)
                        • 9. Re: Java passes object references by value.
                          807588
                          Hey,

                          One last doubt ...

                          When i say

                          String s="Hello World";

                          so s holds the reference of the String type object whose value is "Hello World"

                          am I getting it right?
                          • 10. Re: Java passes object references by value.
                            796440
                            virupatil wrote:
                            Hey,

                            One last doubt ...

                            When i say

                            String s="Hello World";

                            so s holds the reference of the String type object whose value is "Hello World"

                            am I getting it right?
                            No.

                            The variable s has a value. That value is a reference. That reference points to an object. That object is a String with the contents "Hello World".

                            The reference does NOT hold the object's value.
                            • 11. Re: Java passes object references by value.
                              800308
                              virupatil wrote:
                              so s holds the reference of the String type object whose value is "Hello World"
                              am I getting it right?
                              I concurr, I guess... but jverd's definition is much clearer.

                              Now... You understand what pass-by-value means, don't you?
                              • 12. Re: Java passes object references by value.
                                807588
                                ya got it very much clear ..thanks

                                if i get something that disturbs me with this i will surely post it.

                                thanks.