5

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.

1
  • I'm curious what possible use you hope to make of the answer. Commented Jan 19, 2010 at 21:47

3 Answers 3

6

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

2

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.

3 Comments

You're probably correct. But what happens when serialization and communication between JVMs come into play? Regardless of how values are represented internally, there must be a binding standard to allow different JVMs to recognize each others' boolean (and other) values, right?
thats an interesting point Carl. there has to be some binding standard as you said...but I am afraid I cant comment on that..I do not know
No, it's not vendor specific - false is 0, true is 1. Example: compile this class, decompile it with javap -p -c: class Test { boolean t() {return true;} boolean f() {return false;} }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.