GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Size of bool variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Size of bool variable" Watch "Size of bool variable" New topic
Author

Size of bool variable

Mehul Sanghvi
Ranch Hand

Joined: Feb 04, 2002
Posts: 134
Hi All,
I tried and look for size of bool variable in java but was not able to find the same. Can anyone please let me know.
Thanks in advance.
Regards,
Mehul.
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
In Java, you never deal with the byte-size of variables, unless you are serializing them.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Mehul Sanghvi
Ranch Hand

Joined: Feb 04, 2002
Posts: 134
Hi,
I am sorry but I didnt get the gist of what you are trying to say.
Just like int = 32bits, double = 64bits, same way, bool = ?bits ??
Thanks again!
Regards,
Mehul.
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
It's not a meaningful question. A boolean is either true or false. Java does not have C's pointers or sizeof() operator.
If you call the writeBoolean() method of a DataOutputStream, you will write one byte.
[ September 03, 2003: Message edited by: Ron Newman ]
Mehul Sanghvi
Ranch Hand

Joined: Feb 04, 2002
Posts: 134
Thanks Ron!
I get the point that for a user it does not make any difference as bool variable can only store true/false.
What I wanted tot know was how does JVM internally represent it, as a byte or as a bit?
Thanks again.
Regards,
Mehul.
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Since that decision is not visible to you as the user of the JVM, I presume it's implementation-dependent.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

The JVM instruction set actually doesn't contain any boolean-specific instructions; Java compilers use int instructions for them. So booleans in registers are basically 32-bit, like an int.
I've heard it said that it's possible for the JVM to optimize the storage of arrays of boolean to use one byte or even one bit each; don't know if any actually do this or not.


[Jess in Action][AskingGoodQuestions]
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

boolean variable memory size is platform dependent. Many JVMS booleans are implemented internally as ints so this means 4 bytes. So dont think that boolean means less memory.


Groovy
 
GeeCON Prague 2014
 
subject: Size of bool variable