When you assign the value "true" to a boolean data type in Java (e.g. boolean variableName = true;), what value is actually being assigned to that variable?
-
3Take a look at [ask] so you can make your question clearer. The value that is 'actually assigned' is `true`. Unless you are asking something else. – pvg Jun 23 '17 at 19:58
-
1I know it doesn't assign the string "true" to the variable. It isn't the same as String variableName = "true". Typing true/false is simply the syntax for the boolean data type. I am wondering what value is actually getting stored in a boolean variable when you type true/false. – McGrizz Jun 23 '17 at 20:02
-
1I didn't say anything about strings. Again, your question is not particularly clear. – pvg Jun 23 '17 at 20:04
-
1I think the question is reasonably clear in that it's showing curiosity about the internals of how something is actually implemented. I would just add that there are [compliance tests for JVMs](http://openjdk.java.net/groups/conformance/JckAccess/), and even though the VM spec says a `boolean` is stored as an `int`, they probably can't really test for that and some JVM that stored them in, say, 64 bits, could probably still pass the tests. I'm not adding this to my answer as it is purely speculative. – David Conrad Jun 23 '17 at 20:12
-
@DavidConrad There's really no mention of the JVM or internals here, although maybe that's what the poster means. In which case they should say so. As far as Java-the-language is concerned, the value is actually `true`. – pvg Jun 23 '17 at 20:20
-
OP, `true` is not a `String`. It's a `boolean`. The value assigned to a `boolean` value is always either `true` or `false`. Actually. – Lew Bloch Jun 23 '17 at 20:31
2 Answers
The Java Virtual Machine Specification states in section 2.3.4:
Although the Java Virtual Machine defines a boolean type, it only provides very limited support for it. There are no Java Virtual Machine instructions solely dedicated to operations on boolean values. Instead, expressions in the Java programming language that operate on boolean values are compiled to use values of the Java Virtual Machine int data type.
. . .
The Java Virtual Machine encodes boolean array components using 1 to represent true and 0 to represent false. Where Java programming language boolean values are mapped by compilers to values of Java Virtual Machine type int, the compilers must use the same encoding.

- 1
- 1

- 15,432
- 2
- 42
- 54
-
1I'd also add the quote "Arrays of type boolean are accessed and modified using the **byte array instructions baload and bastore.**" – Louis Wasserman Jun 23 '17 at 20:09
-
@Louis Fair enough. I opted to leave out information about boolean arrays since the body of the question was focused on boolean variables. (I also thought of mentioning using `java.util.BitSet` for compact representations of bit vectors and left that out for the same reason.) – David Conrad Jun 23 '17 at 20:14
-
My understanding of the rules of storing booleans is, more or less: `int` on the stack, `byte` on the heap, and the heap is where most people care about memory consumption. – Louis Wasserman Jun 23 '17 at 20:17
-
1The question clearly asked "_in Java_ ..., what value is actually being assigned to that variable?" (emphasis added). The JVM isn't "in Java", it's in bytecode. – Lew Bloch Jun 23 '17 at 20:33
-
@Lew Java runs on the JVM (99.44% of the time). But hey, maybe I'm confused. Maybe he was asking about the Indonesian *island* of Java. If OP wants to clarify, OP can clarify. – David Conrad Jun 24 '17 at 05:37
-
That's great, @Lew, you answer the question as you see fit to interpret it. – David Conrad Jun 24 '17 at 15:21
-
1@Lew Actually, Bytecode doesn't really run on the JVM, since the JVM is just a C++ program compiled to native code running on the CPU. But in most modern CPU architectures the CISC instructions are really implemented in microcode. But the electronics of the CPU depend on quantum mechanics, so it really all runs on QM. – David Conrad Jun 25 '17 at 00:08
-
The actual value assigned to the variable is true
.
But internally, in the virtual machine, booleans may be represented by ints.
From the Java Virtual Machine Specification, section 2.3.4, The boolean
type:
The Java Virtual Machine encodes boolean array components using 1 to represent true and 0 to represent false. Where Java programming language boolean values are mapped by compilers to values of Java Virtual Machine type int, the compilers must use the same encoding.

- 84,978
- 11
- 107
- 151