使用反射更改布尔字符串值不起作用

发布于 2024-12-24 23:17:01 字数 1529 浏览 1 评论 0原文

我正在尝试 Java 反射和内联字符串,并得出了令我困惑的结果。

import java.lang.reflect.Field;

public class HappyDebugging {

    public static void main(String[] args) throws Exception {
        defineTrueFalse();

        System.out.println("true is " + true); // why is it "true is true"?
        System.out.println("false is " + false);
        System.out.println(true);
        System.out.println(false);
        System.out.println("true");
        System.out.println("false");
        System.out.println("true is " + Boolean.valueOf(true));
        System.out.println("false is " + Boolean.valueOf(false));
        System.out.println("true is " + Boolean.valueOf("true"));
        System.out.println("false is " + Boolean.valueOf("false"));
    }

    static void defineTrueFalse() throws Exception{
        Field field = String.class.getDeclaredField("value");
        field.setAccessible(true);
        field.set("true", new char[] {'f', 'a', 'l', 's', 'e'});
        field.set("false", new char[] {'t', 'r', 'u', 'e'});

        field = String.class.getDeclaredField("offset");
        field.setAccessible(true);
        field.setInt("true", 0);
        field.setInt("false", 0);

        field = String.class.getDeclaredField("count");
        field.setAccessible(true);
        field.setInt("true", 5);
        field.setInt("false", 4);
    }
}

为什么输出中的前两行是

true is true
false is false

我希望它们是的

true is false
false is true

请注意,输出在不同平台上有所不同。

I was experimenting with Java reflection and inlined Strings and came up with the result which I find confusing.

import java.lang.reflect.Field;

public class HappyDebugging {

    public static void main(String[] args) throws Exception {
        defineTrueFalse();

        System.out.println("true is " + true); // why is it "true is true"?
        System.out.println("false is " + false);
        System.out.println(true);
        System.out.println(false);
        System.out.println("true");
        System.out.println("false");
        System.out.println("true is " + Boolean.valueOf(true));
        System.out.println("false is " + Boolean.valueOf(false));
        System.out.println("true is " + Boolean.valueOf("true"));
        System.out.println("false is " + Boolean.valueOf("false"));
    }

    static void defineTrueFalse() throws Exception{
        Field field = String.class.getDeclaredField("value");
        field.setAccessible(true);
        field.set("true", new char[] {'f', 'a', 'l', 's', 'e'});
        field.set("false", new char[] {'t', 'r', 'u', 'e'});

        field = String.class.getDeclaredField("offset");
        field.setAccessible(true);
        field.setInt("true", 0);
        field.setInt("false", 0);

        field = String.class.getDeclaredField("count");
        field.setAccessible(true);
        field.setInt("true", 5);
        field.setInt("false", 4);
    }
}

Why are first two lines in the output are

true is true
false is false

I would expect them to be

true is false
false is true

Please note the output varies on different platforms.

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

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

发布评论

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

评论(5

千柳 2024-12-31 23:17:01

这似乎有效......

String.valueOf(BooleanValue)

this seems to be working....

String.valueOf(BooleanValue)
梦途 2024-12-31 23:17:01

在我的编译器中,这两行被编译为使用实际字符串 "true is true""false is false" (也就是说,不会发生运行时串联) ,所以你的反思邪恶来得太晚了。你说输出取决于平台,所以我猜有些编译器一定不会执行这种优化。

In my compiler, those two lines get compiled to use the actual strings "true is true" and "false is false" (that is, no run-time concatenation occurs), so your reflective evil comes too late. You say that the output depends on the platform, so I guess some compilers must not perform this optimization.

最后的乘客 2024-12-31 23:17:01

defineTrueFalse 没有任何效果,因此 "true is " + true 被视为 "true is " + Boolean.toString(true) 这就是为什么它给出结果为 true is true

defineTrueFalse has no effect so "true is " + true is treated as "true is " + Boolean.toString(true) thats why it give you result as true is true

于我来说 2024-12-31 23:17:01

答案很简单。这两行代码

 System.out.println("true is " + true);
 System.out.println("false is " + false);

执行以下操作。
1. 布尔类型“true”的简单值被转换为字符串,结果是字符串“true”。 “假”也一样,就是这样

The answer is simple. These two lines

 System.out.println("true is " + true);
 System.out.println("false is " + false);

do the following.
1. the simple value of type boolean "true" ist converted into String which results in the string "true". The same for "false", and that's it

浊酒尽余欢 2024-12-31 23:17:01

因为你正在搞乱实习字符串文字的内容(我可能会补充说,这种方式会让你的灵魂在地狱的第九圈中遭受永恒的痛苦),但你的前两行连接的是布尔文字,而不是字符串文字。 defineTrueFalse() 中的任何内容对布尔值 truefalse 没有任何影响(与字符串文字 "true"< /code> 和 “假”)。

请注意,不同平台上的输出有所不同。

但我敢打赌,前两行不会。对于可能与字符串相关的东西,因为行为取决于字符串文字的驻留,我认为规范不能保证这一点(因此,地狱的第九圈)。

Because you're messing with the content of interned String literals (in a way that will damn your soul to eternal suffering in the ninth circle of hell, I might add), but your first two lines concatenate boolean literals, not String literals. Nothing in defineTrueFalse() has any effect on the boolean values true and false (as opposed to the String literals "true" and "false").

Please note the output varies on different platforms.

But not for the first two lines, I'd wager. For the String-related stuff that may be, since the behaviour depends on the interning of String literals, which I don't think is guaranteed by the spec (thus, ninth circle of Hell).

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