3

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.

Raedwald
  • 46,613
  • 43
  • 151
  • 237
Vladimir Zhilyaev
  • 175
  • 1
  • 2
  • 11
  • Not sure what this Field thing is but I don't think you are overriding anything since the function (defineTrueFalse()) is void so it's still printing true as true (and false as false) – Cemre Mengü Jan 04 '12 at 14:58
  • Seems like all you're doing is invoking undefined behavior by breaking `String.class` internals. – Matt Ball Jan 04 '12 at 14:59
  • @Cemre RTM http://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Field.html – Matt Ball Jan 04 '12 at 15:00
  • @Cemre: He's not overriding anything. He is fiddling with the field values (`value`, `offset` and `count`) contained inside the `String` objects represented by the literals `"true"` and `"false"`. – Adam Paynter Jan 04 '12 at 15:01
  • Ah I see now. Sorry for misinterpreting the question. Please ignore my comment. Thanks for the replies. – Cemre Mengü Jan 04 '12 at 15:02

5 Answers5

17

this seems to be working....

String.valueOf(BooleanValue)
Gil
  • 568
  • 5
  • 9
6

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.

ruakh
  • 175,680
  • 26
  • 273
  • 307
4

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

Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243
3

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

Hons
  • 3,804
  • 3
  • 32
  • 50
2

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).

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720