Android - 通过引用共享对象

发布于 2024-10-17 22:46:03 字数 453 浏览 3 评论 0原文

嘿人们, 我创建了一个类来扩展应用程序以共享某些变量。然后,在每个活动中,我使用该类的对象(全局状态),如下所示:

gs = (GlobalState) getApplication();

然后声明引用共享变量的局部变量,例如:

boolean localStr = gs.str;

现在我的印象是,这将传递一个引用,因此对 localStr 的任何更改都将是反映在 Globalstate 内的 str 中。然而,情况似乎并非如此。

在这个庄园中创建的一些变量(例如对象数组)似乎作为引用传递,因此任何更改都会反映在全局状态中,但是对于几乎所有其他变量(布尔值、字符串和整数),似乎这些更改都没有反映,因此被复制而不是被引用。

我是否做错了什么,或者这就是android的工作原理,如果是这样,我该如何通过引用传递?谢谢。

hey people,
I have created a class which extends application to share certain variables. Within each activity I then use an object of that class (globalstate) as so:

gs = (GlobalState) getApplication();

I then declare local variable which reference the shared ones such as:

boolean localStr = gs.str;

Now I am under the impression this would be passed a a reference and therefore any change to localStr would be reflected in str within globalstate. However this does not seem to be the case.

Some variables created in this manor such as an array of object seem to be passed as reference and therefore any changes are reflected within globalstate however for almost every other variables (booleans, strings & ints) it seems the changes aren't reflected and therefore are being copied rather than referenced.

Have I done something wrong or is this how android works, if so how can i pass by reference? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

成熟的代价 2024-10-24 22:46:03

boolean 是一种值类型,Java 不使用引用来存储它。对于所有 Java 基本类型(int、long、...)都是一样的。字符串则不同,它们是引用类型,但如 Johan Sjöberg 所解释的那样是不可变的。

这就是为什么在使用 ArrayList(例如)时,如果您尝试使用像 new ArrayList() 这样的值类型,您将会收到错误。但是 new ArrayList() 可以工作,因为 Boolean 是引用类型,而不是值类型。

为了避免出现问题,您应该在任何地方使用引用(使用 gs.str 而不是创建局部变量 localStr)。如果你不太明白我在说什么,我可以更好地解释。

boolean is a value type, Java doesn't use a reference to store it. It's the same for all Java primitive types (int, long, ...). Strings are different, they are reference types but are immutable as explained by Johan Sjöberg.

That's why when using an ArrayList (for example) you will get an error if you try to use a value type like new ArrayList<boolean>(). But new ArrayList<Boolean>() will work because Boolean is a reference type, not a value type.

To avoid your problem you should use your reference everywhere (use gs.str instead of creating a local variable, localStr). I can explain it better if you don't really understand what I'm saying.

放低过去 2024-10-24 22:46:03

这不仅是 android 的工作原理,也是 java 的工作原理。字符串是不可变的,这意味着它们不能更改。每次尝试修改字符串时,都会创建一个新字符串;因此,以下代码将使名为 first 的字符串保持不变:

String first = "first";    // First points to mem address e.g., 0x1
String second = first;     // Second also does
second = "something else"; // Second now points to 0x2, first still to 0x1

对于所有基本类型(int、boolean 等)来说,它的行为完全相同。

但是,当您传递对象列表时,列表内容的更改将随处更改。这是因为您的对象共享相同的列表引用(它本身不会改变,只有它的内容)。

That's not only how android works, that's how java works. Strings are immutable, meaning they cannot be changed. Every time you try and modify a string a new string is created; hence the following code will leave the string named first unchanged:

String first = "first";    // First points to mem address e.g., 0x1
String second = first;     // Second also does
second = "something else"; // Second now points to 0x2, first still to 0x1

It's exactly the same behaviour for all primitive types (int, boolean etc).

However, when you pass a List of objects, alterations of the list contents will be changed everywhere. This is since your objects share the same list reference (which itself doesn't change, only it's contents).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文