tags:

views:

205

answers:

8

Hi,

We know Java supports only "pass by value" .If I pass a collection for eg., a hash table to a function then the modifications to this collection inside the function should not be updated outside this function.But this is not the case in Java?.How do we conclude this?

Please anyone conclude this discussion with a proof...

+8  A: 

Passing an object by value to a method means that the method is given a copy of the reference to the object, but it is still possible to access the object members from within the passed reference copy. In the case of collections, this includes invoking the methods to add and remove objects, and of course to modify the contained objects themselves.

Konamiman
+2  A: 

Though its "pass by value"- In case of primitives variables the value of the variable is copied to the function. In case of object references, which in case of Hash table is true, the value within the reference which is nothing but the address to the object on the heap is passed. So the reference with in the function to which u are passing the "hash table" is actually getting a copy of the address of the hash table object. So what ever changes made from with in the function are reflected in the main object.

sana
A: 

Here is a great write up with some examples. http://javadude.com/articles/passbyvalue.htm

Here is my simple take on the issue. Java is always a pass-by-value language. It helps to think of it as a pass-by-reference language when talking about objects. BUT this is not entirely true. What it is doing is passing the value of the memory address.

EDIT: Thanks for the down votes. Maybe you should have read the article first. Since no one has a will take the liberty to give an example:

public class Dog {

    public String name;

    public Dog(String name) {
        this.name = name;
    }

    public static void main(String []args) {

        Dog newDog = new Dog("Max");
        newDog.test(newDog);
        System.out.println(newDog.name);

    }

    public void test(Dog dog) {
        dog = new Dog("Gotcha");
    }

}

So what does the above output to the console? Now if you guys screaming pass-by-reference were correct it would print "Gotcha" but it doesn't it prints "Max". But wait, that is exactly like pass-by-value behaviour. But why? Because in Java Objects are not passed by reference. A correct statement would be Object references are passed by value.

uriDium
HI uriDium,thanks for ur comments ... but it is confusing me ..see all the other answer they are in sync but urs only seems different and confusing.Could u please explain it clearly?
Sidharth
your explanation described the opposite of your claim, when passing the object's address your are in fact passing it by reference
Eli
@Eli - I have added an example.
uriDium
+2  A: 

If you want "pass by value" behaviour for objects, you can pass the method a clone of the object, so even if the method changes the passed object, the original object remains unmodified.

Sylar
Or use Collections.unmodifiable* methods like Collections.unmodifiableList. See http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collections.html
Harold L
However, in both cases, the component objects are still passed by reference, so they can be modified.
Thilo
True, so the solution would be to fill the component with clones as well.
Sylar
+2  A: 

You are confusing what "value" means. If you are dealing with objects, this means that any changes of the value of the reference are not reflected to the outside. A straightforward example that shows this behavior is be something like this:

public void foo() {
  String s = "example";
  String param = s;
  bar(param);
  assert s == param : "The value of parameter reference was not modified";
  assert param.equals("example");
}

public void bar(String param) {
  param = "something different";
}

Passing by value does, however, not mean that you cannot change the internal state of the object. This is perfectly possible and has consequences to the outside:

public void foo() {
  List<String> l = Arrays.asList("example");
  List<String> param = l;
  bar(param);
  assert s == param : "The value of parameter reference was not modified";
  assert !param.get(0).equals("example") : "bar changed the internal state of the parameter";
  assert !l.get(0).equals("example") : "bar changed the internal state of the list that is reference equal to the parameter";
}

public void bar(List<String> param) {
  param.set(0, "something different");
}

Of course, such changes of the internal state of an object are reflected up all the way in the caller stack, which might lead to unforeseen side effects. This is the reason why the immutable object pattern was introduced. In the Java standard library, java.lang.Object and java.lang.String are examples for classes that implement this pattern. However, Java does not provide a language feature where where you can state that a class implements this pattern, so you must rely on JavaDoc and/or source code of the class to be sure an object is immutable.

nd
A: 

what is the point of this discussion?

if you wanna protect your arguments passed to other methods juz follow Thilo's comment and thats it

Dapeng
+1  A: 

In Java you never pass Objects, you only pass references/pointers to Objects. So in a call

Object x = new Object();
int y = 42;

foo(x,y)

the value of x, i.e. the (reference to) the Object, is passed, as well as the value of y, the integer 42. "Pass by reference" would mean that you can change the value of the variables, x and y, withing foo().

mfx
+1  A: 

A proof:

public static void main(String[] args) {
    int i = 1;
    addOne(i);
    System.out.println("after addOne: " + i);

    // now for Objects
    String text = "a text";
    addText(text);
    System.out.println("after addText: " + text);
}


private static void addOne(int i) {
    i += 1;
    System.out.println("in addOne: " + i);
}

private static void addText(String text) {
    text += ", more text";
    System.out.println("in addText: " + text);
}

should result in

in addOne: 2
after addOne: 1
in addText: a text, more text
after addText: a text

as you can see the variables in main do not get changed, that is, the variable is passed by value.

Carlos Heuberger
Why the downvote? not much helpful downvoting without commenting...
Carlos Heuberger