• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need clarification on boolean in "C" and Java programming language

 
karthikeyan sai
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In "C" programming language Boolean values are commonly represented by integers but in java Boolean variables are represented by the primitive type boolean
why two different representation?
is there any specific reason java implemented boolean variable as primitive type boolean?
Could anyone please give the reason?
 
Henry Wong
author
Marshal
Pie
Posts: 21427
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
krthik s.au wrote:In "C" programming language Boolean values are commonly represented by integers but in java Boolean variables are represented by the primitive type boolean
why two different representation?
is there any specific reason java implemented boolean variable as primitive type boolean?
Could anyone please give the reason?


It's not 2 different representations. Java has a boolean type. And C doesn't. C uses the preprocessor to simulate it. It doesn't support the boolean type.

IMO, supporting the boolean type is a good thing. Because of it, the compiler can type check loop conditions and conditional statements -- no more accidentally using an assignment instead of a comparison operator.

Henry

 
Campbell Ritchie
Sheriff
Pie
Posts: 49827
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote: . . . no more accidentally using an assignment instead of a comparison operator. . . .
Agree, but it is still occasionally possible to write = true when you meant to write == true, so never write == true or == false.
 
Embla Tingeling
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
krthik s.au wrote:is there any specific reason java implemented boolean variable as primitive type boolean?


The reason is type safety. It's always safer to have a dedicated type, than to use a general type for lots of different things.

Note that although much of the Java syntax was based on C (for reasons of familiarity) there was never the idea that Java should adopt C completely. On the contrary. The idea was that Java should avoid unsafe and error-prone concepts of other languages.
 
karthikeyan sai
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adding to the discussion, This is taken from the jvm spec
"boolean values are compiled to use values of the Java virtual machine int data type. "
this states that it intend to use as int data type.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49827
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What it means is that a boolean is converted to a number, but only where you can't see it. BCEL is probably a bit out of date, but you might find something useful there. Try ctrl-F --- boolean.
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only place where the size of a boolean is fixed is in serialized form. Internally, a JVM implementation can choose anything they want for a boolean; an int with only values 0 and 1 (or 0 and -1, so that if you flip all bits of true you get false and vice versa), or use one int (32 bits) to store up to 32 booleans, one bit for each boolean.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49827
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember that everything in a computer is a number. Everything is stored in binary format, which only stores numbers. There is no such thing as a "true" or a "false" or (in chars) an 'a' or a 'b'. Only numbers. 1s an 0s.
 
Embla Tingeling
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Remember that everything in a computer is a number. Everything is stored in binary format, which only stores numbers. There is no such thing as a "true" or a "false" or (in chars) an 'a' or a 'b'. Only numbers. 1s an 0s.


That's true only for digital computers.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49827
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Who uses analogue computers any more?
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anybody with a brain?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49827
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not me then . . .
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15448
42
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting.


Polish analog computer AKAT-1.
(From Analog computer on Wikipedia).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic