as we know in java variables are bit holders with a designated type. And for primitives the bits represents a numeric value.
For example. a byte with value 6 has a bit pattern 00000110.
so i wanted to know as boolean is also a primitive what is the bit pattern for it for value true and false.
Internally in the bytecode/VM booleans are represented as bytes with the bit patterns 00000001 for true and 00000000 for false. But that information doesn't buy you anything as a Java developer as you simply can't access or otherwise use the numerical represantation of booleans in Java as Java has them strictly seperated from numbers.
Edit: I just looked up the Java VM Spec again and found out my answer was wrong. Contrary to what I said before, booleans are stored as CONSTANT_Integer structs in byte code which makes them occupy 4 bytes for data in the constant pool, but as the constant pool is unified, there can be at most 2 boolean entries in any class. And since a reference to the constant pool is always 2 bytes wide, a array of booleans would occupy 2 bytes per entry in the byte code.
Yes, as skaffman said: True is 1 and false is 0.
But that doesn't really matter, because unless you look at serialized data outside your program, you're unlikely to ever see those values in the wild.
For what it's worth, common Java implementations actually store booleans in integer-sized fields, i.e. they have 31 zero's worth of padding around that single bit of information. Accessing 32 bits at a time is a little bit faster than grabbing one byte from among 4, and much faster than digging a single bit out of a byte. This optimization makes arrays of booleans annoyingly large, though. If you have many, many bits to deal with and need to cut down on space, you're better off using BitSet
.
If I am not wrong JVM specification does not mandate how the boolean values have to be represented internally. How the value is represented internally is vendor specific and does not matter to the programmer as this detail is totally transparent to programs running over the JVM.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With