• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

width of boolean  RSS feed

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is 1 bit
 
Chitra Jay
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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."
 
Nazmul Huda Sarkar
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmmmm......yet so manythings to know
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}
}
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!