aspose file tools*
The moose likes Java in General and the fly likes Need clarification on boolean in Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Need clarification on boolean in "C" and Java programming language" Watch "Need clarification on boolean in "C" and Java programming language" New topic
Author

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

karthikeyan sai
Greenhorn

Joined: Oct 07, 2009
Posts: 8
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
Sheriff

Joined: Sep 28, 2004
Posts: 19004
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
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

Joined: Oct 22, 2009
Posts: 237
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

Joined: Oct 07, 2009
Posts: 8
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

Joined: Oct 13, 2005
Posts: 39885
    
  28
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

Joined: Oct 27, 2005
Posts: 19762
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
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

Joined: Oct 22, 2009
Posts: 237
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

Joined: Oct 13, 2005
Posts: 39885
    
  28
Who uses analogue computers any more?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

Anybody with a brain?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
Not me then . . .
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14352
    
  22

Interesting.


Polish analog computer AKAT-1.
(From Analog computer on Wikipedia).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need clarification on boolean in "C" and Java programming language