aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes width of boolean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "width of boolean" Watch "width of boolean" New topic
Author

width of boolean

Chitra Jay
Ranch Hand

Joined: May 02, 2002
Posts: 76
what is the width(in bits) of boolean datatype? Some tutorials(incl R&H) says 1 bit.but,the book by khalid mughal says "not applicable".i find it contradictory.
Nazmul Huda Sarkar
Ranch Hand

Joined: Feb 01, 2002
Posts: 317
It is 1 bit


Nazmul<br />SCJP,SCWCD,IBM OOAD with UML
Chitra Jay
Ranch Hand

Joined: May 02, 2002
Posts: 76
Thanks a lot
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
The JVM specification 2.4.1
doesn't say the width in bits for a boolean. I think the size of a boolean is left to the implementator of the virtual machine.
Page 140 in the book Inside The Java 2 Virtual Machine by Bill Venners:
"
When a compiler translates Java source code into bytecodes, it uses int o bytes to represents booleans.
Array of booleans are accessed as arrays of byte, although it can be represented in the heap as arrays of bytes or as bit fields."


SCJP2. Please Indent your code using UBB Code
Nazmul Huda Sarkar
Ranch Hand

Joined: Feb 01, 2002
Posts: 317
Hmmmmm......yet so manythings to know
Doanh Nguyen
Ranch Hand

Joined: Dec 02, 2000
Posts: 45
This code seems to suggest that it's 1 byte long:
import java.io.*;
public class Java {
public static void main(String[] args) throws IOException{
RandomAccessFile raf = new RandomAccessFile("test.dat","rw");
raf.writeInt(1);
raf.writeShort(2);
raf.writeByte(3);
System.out.println(raf.length());
boolean b = true;
raf.writeBoolean(b);
System.out.println(raf.length());
raf.seek(raf.length()-1);
b = raf.readBoolean();
System.out.println(b);
}
}
Asif Masood
Greenhorn

Joined: Dec 13, 2000
Posts: 20
The size of the boolean is 1 byte (8-bits) as specified in the section of sizes for data types in RHE book.
Asif
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Doanh we mean the size of a boolean within the JVM.
This is from JVMS 3.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 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.
Doanh Nguyen
Ranch Hand

Joined: Dec 02, 2000
Posts: 45
Originally posted by Jose Botella:
Doanh we mean the size of a boolean within the JVM.
This is from JVMS 3.3.4

Yes , many computer languages use only one bit to handle binary conditions such as on or off, true or false, yes or no... From the storage viewpoint, it takes a little bit more than one bit to hold that one bit. In fact, it takes 8 bits. Languages with bit-manipulating capability used to pack up to eight boolean bits in one byte to save storage.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: width of boolean