0

*strong text*what is reference copying technique?

The reference copying technique is much more difficult to use for mutable objects, because if any user of a reference to a mutable object changes it, all other users of that reference will see the change

Another thing I tested sample code below for pass-by-value & pass-by-reference.The primitive values are pass-by-value and object reference are pass-by-reference.In the sample example,I tested types a string contant,String object,String buffer,int,ArrayList.

          String s="foo";
    String sample1=new String("dog");
    StringBuffer sb=new StringBuffer();
    sb.append("abc");
    ArrayList l=new ArrayList();
    l.add("ssss");
    l.add("bbbb");
    l.add("ssbbbss");
    l.add("bbbb");
    l.add("bbbb");
    int k=14,listsize=0;
    listsize=l.size();
    TesingPrimitivRefernce.generateString(s);
    TesingPrimitivRefernce.generateString(sample1);
    TesingPrimitivRefernce.generateStringBuilder(sb);
    TesingPrimitivRefernce.generateInt(k);
    TesingPrimitivRefernce.generateNewList(l);
    System.out.println("String============"+s+" String Buffer========"+sb+" String object "+sample1);
    System.out.println("int Primitive Values==="+k);
    System.out.println("Orignal List Size"+listsize+" After called method List Size=========="+l.size());


 public static void generateString(String s){
       s=s.concat("d");
   }
   public static void generateStringBuilder(StringBuffer s){
       s=s.append("d");

   }
   public static void generateInt(int s){
       s=10;

   }
   public static void generateNewList(ArrayList list){
      list.remove("bbbb");

   }

My doubt is why String object is not changed (**i.e.,variable sample1) after calling *TesingPrimitivRefernce.generateString(sample1).but in the arraylist i removed the value "bbb" and automatically decrase size.Here I passed String object reference.So why it is not changed?*

user1357722
  • 7,088
  • 13
  • 34
  • 43

3 Answers3

1

I don't know who told you that "primitive values are pass-by-value and object reference are pass-by-reference". This is wrong.

In Java, everything is pass-by-value.

Now, objects are not manipulated directly but through references. Multiple references can point to the same object but these references are copied like any other arguments.

Vincent Robert
  • 35,564
  • 14
  • 82
  • 119
1

References in Java are passed by value. So if you reassign the first argument in TesingPrimitivRefernce.generateString to something else, it is the copy of the sample1 reference that is changed (you weren't trying to modify the argument using some... modifiers, were you? :)).

So, before the modification:

  • sample1 -> dog
  • s -> dog

After the modification:

  • sample1 -> dog
  • s -> dogdd (and this String object is lost once the method exits).

In the ArrayList case, you were modifying the underlying list, so the originally referenced object was modified.

Alexander Pavlov
  • 31,598
  • 5
  • 67
  • 93
0

Strings are constant; their values cannot be changed after they are created. At the moment you pass a string to a method a copy of this string is created. All further changes in this method modify this copy, not the string itself.