Java Pass error by value?

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

Please someone conclude this discussion with proof ...

+3
source share
9 answers

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

+8

, "". , , . , , :

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";
}

, , , . :

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");
}

, , . . Java java.lang.Object java.lang.String , . Java , , , JavaDoc / , , .

+4

" " , clone , , .

+3

" " - . , Hash , , , . , , u "-", -. , , .

+2

Java , / .

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

foo(x,y)

x, .. ( ) Object, y, 42. " " , , x y, foo().

+1

:

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);
}

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

, main , .

+1

Java , , get pass to method, .

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";
}

}

Cat

, ( ) .

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

,

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";
}

}

.

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



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

,

0

. http://javadude.com/articles/passbyvalue.htm

. Java . , . . , , .

EDIT: . , . :

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 to bring to the console above? Now, if you guys screaming at the link were correct, he would print "Gotcha", but he does not print "Max." But wait, this is exactly the same as turn-based behavior. But why? Because in Java, objects are not passed by reference. The correct statement will be references to objects passed by value.

-1
source

What is the point of this discussion?

if you want to protect your arguments passed to other methods, juz follows the comment of Thilo and thats it

-1
source

Source: https://habr.com/ru/post/1725933/


All Articles