4

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...

2
  • 3
    What you want are immutable collections (and component elements). Automatically thread-safe, too. Collections.unmodifiableList is a good first step. Commented Dec 17, 2009 at 9:13
  • I have added an example for you. Commented Dec 18, 2009 at 13:14

9 Answers 9

8

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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.

Comments

3

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.

3 Comments

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

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.

Comments

1

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().

Comments

1

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.

Comments

0

Java is pass by value but when we pass object refrence , reference value is get pass to method and from there method can change the value of object members.

Look at following code

public class Passbyvalue {

public static void main(String[] args) {
    // TODO code application logic here
    Animal animal=new Animal();
    animal.name="Dog";
    System.out.println(animal.name);
    testIt(animal);
    System.out.println(animal.name);
}


public static void testIt(Animal animal){
    animal.name="Cat";
}

}

Output

Dog Cat

This is because both reference( Original and method) poiting to same object.

    __________
   |          |                 
   |          |<------------- Orignal Refernce 
   | Object   |   
   |          |<------------- Method Refernce
   |          |
   |__________|

If you want to see this effect more clearly create new object in method

public class Passbyvalue {

public static void main(String[] args) {
    Animal animal=new Animal();
    animal.name="Dog";
    System.out.println(animal.name);
    testIt(animal);
    System.out.println(animal.name);
}


public static void testIt(Animal animal){
    animal=new Animal();
    animal.name="Cat";
}

}

Output Dog Dog

Now method reference is poting to other Object in heap.

    __________
   |          |                 
   |          |
   | Orignal  |<------------- Orignal Refernce    
   | Object   |
   |          |
   |__________|



    __________
   |          |                 
   |          |
   | Method   |<------------- Method Refernce    
   | Object   |
   |          |
   |__________|

Hope this will help

Comments

-1

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

Comments

-1

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.

3 Comments

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?
your explanation described the opposite of your claim, when passing the object's address your are in fact passing it by reference
@Eli - I have added an example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.