Size of boolean datatype

hi all, Can anyone tell me, how many bytes are taken by a boolean variable. Pls support ur answer. Also let me know, Is there any method in java to calculate the size of a particular datatype, like sizeof() in c/c++. Thanks in advance.
[279 byte] By [Rajesh_Pratapgarha] at [2007-10-2 21:22:52]
# 1
The size of a boolean is 1 bit 1 = true0 = false
Karthikeyan_Vaithilingama at 2007-7-14 0:33:21 > top of Java-index,Java Essentials,New To Java...
# 2
Although a boolean can be represented by a single bit, I have seen various suggstions as to how much memory a boolean does occupy. I've seen 1 bit, 32 bits and JavaRanch suggest it is a byte.
floundera at 2007-7-14 0:33:21 > top of Java-index,Java Essentials,New To Java...
# 3
As I know, size isn't specified and may be jvm dependant.Why do you need sizes for all?
s-e-r-g-ea at 2007-7-14 0:33:21 > top of Java-index,Java Essentials,New To Java...
# 4
Refer the following link http://www.cafeaulait.org/course/week2/02.html
Karthikeyan_Vaithilingama at 2007-7-14 0:33:21 > top of Java-index,Java Essentials,New To Java...
# 5

> Can anyone tell me, how many bytes are taken by a

> boolean variable.

undefined by the language standard.

> Pls support ur answer.

fumble wumble doodle.

> Also let me know, Is there any method in java to

> calculate the size of

> a particular datatype, like sizeof() in c/c++.

>

no.

jwentinga at 2007-7-14 0:33:21 > top of Java-index,Java Essentials,New To Java...
# 6

> Although a boolean can be represented by a single

> bit, I have seen various suggstions as to how much

> memory a boolean does occupy. I've seen 1 bit, 32

> bits and JavaRanch suggest it is a byte.

The size is not specified in the language (JLS) but it's specified in the VM specification, and that specification says (if I remember it correctly) that a boolean has the size of an int if it's a variable/attribute, but a boolean in an array has the size of a byte.

Kaj

kajbja at 2007-7-14 0:33:21 > top of Java-index,Java Essentials,New To Java...
# 7

> Refer the following link

>

> http://www.cafeaulait.org/course/week2/02.html

Yes, that website mentions 1 bit but as I said other sites mention other sizes. Just because you found it on that website doesn't mean it is true. Only the good people at sun know the truth.

floundera at 2007-7-14 0:33:21 > top of Java-index,Java Essentials,New To Java...
# 8
I think one bit is enough for boolean.Can u send me the other links that says the size of the boolean
Karthikeyan_Vaithilingama at 2007-7-14 0:33:22 > top of Java-index,Java Essentials,New To Java...
# 9

> > Refer the following link

> >

> > http://www.cafeaulait.org/course/week2/02.html

>

> Yes, that website mentions 1 bit but as I said other

> sites mention other sizes. Just because you found it

> on that website doesn't mean it is true. Only the

> good people at sun know the truth.

Ok, I will quote the VM specification which you have to adhere to if you implement a VM. As I said previously: The size is not specified in the JLS, but in the VM specification and you will not have a compatible VM if you aren't following it.:

http://java.sun.com/docs/books/vmspec/2nd-edition/html/Overview.doc.html

"3.3.4 The boolean Type

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 does directly support boolean arrays. Its newarray instruction enables creation of boolean arrays. Arrays of type boolean are accessed and modified using the byte array instructions baload and bastore.2

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

Kaj

kajbja at 2007-7-14 0:33:22 > top of Java-index,Java Essentials,New To Java...
# 10
Google: Java primitive size.<exageration>I think you will find each site claims a different size.</exageration>
floundera at 2007-7-14 0:33:22 > top of Java-index,Java Essentials,New To Java...
# 11
> I think one bit is enough for boolean.> > Can u send me the other links that says the size of> the booleanRead my replies.
kajbja at 2007-7-14 0:33:22 > top of Java-index,Java Essentials,New To Java...
# 12
Hi kajbj,I think this information related to specific version of Sun JVM and may be changed in another version or be implemented another way in other vendor jvm.
s-e-r-g-ea at 2007-7-14 0:33:22 > top of Java-index,Java Essentials,New To Java...
# 13

> Hi kajbj,

> I think this information related to specific version

> of Sun JVM and may be changed in another version or

> be implemented another way in other vendor jvm.

I think you are wrong. From the preface of the specification:

The Java virtual machine specification has been written to fully document the design of the Java virtual machine. It is essential for compiler writers who wish to target the Java virtual machine and for programmers who want to implement a compatible Java virtual machine. It is also a definitive source for anyone who wants to know exactly how the Java programming language is implemented.

Kaj

kajbja at 2007-7-14 0:33:22 > top of Java-index,Java Essentials,New To Java...
# 14

> > Hi kajbj,

> > I think this information related to specific

> version

> > of Sun JVM and may be changed in another version

> or

> > be implemented another way in other vendor jvm.

>

> I think you are wrong. From the preface of the

> specification:

>

>

> The Java virtual machine specification has been

> written to fully document the design of the Java

> virtual machine. It is essential for compiler writers

> who wish to target the Java virtual machine and for

> programmers who want to implement a compatible Java

> virtual machine. It is also a definitive source for

> anyone who wants to know exactly how the Java

> programming language is implemented.

>

>

> Kaj

...which means that it's implemented in the same way by other vendors, and it's not likely that the sizes will change (it's about as likely as changing the size of an int)

kajbja at 2007-7-14 0:33:22 > top of Java-index,Java Essentials,New To Java...
# 15
Thanks for explanation :)
s-e-r-g-ea at 2007-7-21 8:03:32 > top of Java-index,Java Essentials,New To Java...
# 16
> The size of a boolean is 1 bit > > 1 = true> 0 = falseand how do you store exactly one bit in memory?
georgemca at 2007-7-21 8:03:32 > top of Java-index,Java Essentials,New To Java...