6 Replies Latest reply: Aug 11, 2008 10:41 AM by 3004 RSS

    Simple pass by value example using String object

    843785
      hi.. I was just wondering why wasn't the result was "John Doe" instead of "John". I know that Java is pass by value and for the primitive type parameter, the value of the calling argument cannot be changed no matter how you implement. But String is an object and with the implementation like this; shouldn't it change the value of the calling argument? I would think that both instance field "name" and parameter "n" would be pointing to the same thing in the heap. Please explain..
      Is it because String objects are immutable? well.. I don't really know... thanks



      public class TestProgram{
           public static void main(String[] args){
                Employee e = new Employee("John");
                e.add();
                System.out.println(e.getName());
           }
      }


      class Employee
      {

           private String name;

           Employee(String s){
                this.name = s;
                addLastName(this.name);
           }

           public void setName(String s)
           {
                name = s;

           }

           public String getName()
           {
                return name;
           }


           public void add(){
                addLastName(this.name);
           }

           private void addLastName(String n){
                n = "John Doe";
      }
      }
        • 1. Re: Simple pass by value example using String object
          843785
          private void addLastName(String n){
              n = "John Doe";
          }
          You are changing the parameter of the method and not the state of your Employee object (ie the name variable).
          • 2. Re: Simple pass by value example using String object
            843785
            I have two stories from the JavaRanch for you to read:

            [Cup Size|http://www.javaranch.com/campfire/StoryCups.jsp]
            [Pass-by-Value Please (Cup Size continued)|http://www.javaranch.com/campfire/StoryPassBy.jsp]

            These should answer your questions. Good luck.
            • 3. Re: Simple pass by value example using String object
              3004
              java_us wrote:
              hi.. I was just wondering why wasn't the result was "John Doe" instead of "John".
              Because references are passed by value. Objects are not passed at all. And nothing is passed by reference.

              [http://javadude.com/articles/passbyvalue.htm]
              [http://java.sun.com/developer/JDCTechTips/2001/tt1009.html#tip1]
              [http://www.javaranch.com/campfire/StoryPassBy.jsp]
              [http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html]
              [http://www-106.ibm.com/developerworks/library/j-praxis/pr1.html]
              [http://www.cs.toronto.edu/~dianeh/tutorials/params/]
              [http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#38698]
              [http://radio.javaranch.com/channel/val/2004/05/21/1085125887000.html]

              =
              • 4. Re: Simple pass by value example using String object
                843785
                I thought Java made a copy of the reference when passing, i.e. it will create another copy of the address and make it point to the same reference in the heap.
                For instance:
                Employee staff1 = new Employee("John");
                some_method(staff1);
                System.out.println(staff1.name);

                Consider:
                public void some_method(Employee some_staff){
                some_staff.name = "Java"
                }

                The method will have a copy of the reference to the Employee object with name "John".

                So, changing "some_staff" name to "Java" will also affect the "staff1" because the both have the same reference to the object; and when print out staff1.name,it will print out "Java". I understand up to this point. But String is an object and located in heap? If so, why isn't there is side affect done by the method as if it is with the Employee object in the example. Thanks...
                • 5. Re: Simple pass by value example using String object
                  843785
                  java_us wrote:
                  public void some_method(Employee some_staff){
                  some_staff.name = "Java"
                  }
                  Not the difference between that code and what you wrote earlier:
                  private void addLastName(String n){
                      n = "John Doe";
                  }
                  • 6. Re: Simple pass by value example using String object
                    3004
                    java_us wrote:
                    I thought Java made a copy of the reference when passing,
                    It does.
                    i.e. it will create another copy of the address and make it point to the same reference in the heap.
                    No, it makes a copy of the reference, so both references point to the same object. Nothing ever points to a reference in Java.

                    Note that if you're thinking "reference" in the C++ sense, that's not what Java references are. Java references are more like C++ pointers than C++ references.

                    Edited by: jverd on Aug 11, 2008 8:40 AM