gpt4 book ai didi

Java 引用类型

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:43:36 26 4
gpt4 key购买 nike

Java 如何处理传递引用数据类型参数??谁能给个清晰的图?

最佳答案

Java 将引用的副本传递给方法。引用仍然指向同一个实例。

想象一下,如果我有一张写有餐厅地址的纸条。你也想去同一家餐馆,所以我拿一张新纸条,把餐馆的地址抄在纸条上给你。两张纸条都指向同一家餐厅,但它们是对该实例的不同引用

餐厅本身没有重复,只是对它的引用是重复的。

乔恩双向飞碟 provides a similar analogy :

The balloon analogy

I imagine every object as a helium balloon, every reference as a piece of string, and every variable as something which can hold onto a piece of string. If the reference is a null reference, that's like having a piece of string without anything attached to the end. If it's a reference to a genuine object, it's a piece of string tied onto the balloon representing that object. When a reference is copied (either for variable assignment or as part of a method call) it's as if another piece of string is created attached to whatever the first piece of string is attached to. The actual piece of string the variable (if any) is holding onto doesn't go anywhere - it's only copied.

这是一个例子:

// 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
}

关于Java 引用类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2677979/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com