• Post Reply Bookmark Topic Watch Topic
  • New Topic

How java store the boolean value  RSS feed

 
Mohan Giramkar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

please tell me how boolean value store in java internallly.

Mohan
 
Nigel Browne
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a literal boolean is stored as either true or false
 
Mahesh Bhatt
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

whenever you declare a boolean, its default value as a literal 'false' is stored in the memory(if its a class variable) otherwise its stored as a literal in the memory with the value that you initialized it with. If you have a background of c++ please note that in java its either 'true' or 'false' not 0 or 1 as its in c++.
 
Edwin Keeton
Ranch Hand
Posts: 214
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Physically, they are stored internally as integers, 1 for true and 0 for false. Unless you're writing a JVM this knowledge is of no use other than to satisfy curiosity.

As was already said, the literal values true and false are the only valid values for a boolean primitive in the language.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From section 3.3.4 The boolean Type of the JVM specification:
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!