Java Link Type

How does Java work with passing arguments like reference data? Can someone give a clear picture?

+3
source share
2 answers

Java passes a copy of the method reference. The link still points to the same instance.

Imagine if I had a sheet of paper with the address of the restaurant. You also want to go to the same restaurant so that I receive a new sheet of paper and copy the restaurant address onto this paper and give it to you. Both sheets of paper point to the same restaurant, but they are separate references to the copy.

The restaurant itself is not duplicated, only a link to it is duplicated.

Jon Skeet provides a similar analogy :

Balloon analogy

, , . , , , . , , , . ( ) , . string ( ) - .

:

// Here I have one instance and one reference pointing to it
Object o = new Object();
// At this moment a copy of "o" is made and passed to "foo"
foo(o);

void foo(Object obj) {
    // In here I have obj which is a copy of whatever
    // reference was passed to me
}
+10

Java (, , int, float, boolean ..), .

, :

Foo f = new Foo();

, f Foo. :

void doSomething(Foo myFoo) { ... }

doSomething(f);

doSomething() , f. , doSomething() f, .

++, , . ( ).

, ++ Java, .

+1

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


All Articles