Comparison of boolean data type in C++ and Java
The Boolean data type is one of the primitive data types in both C++ and Java. Although, it may seem to be the easiest of all the data types, as it can have only two values – true or false, but it surely is a tricky one as there are certain differences in its usage in both Java and C++, which if not taken care, can result in an error. The difference in its usage in C++ and Java are-
- Declaration: The declaration of boolean data type in C++ involve the use of keyword bool, whereas declaration in Java is done by keyword boolean.
C++ Code:
#include<iostream>
using namespace std;
int main()
{
bool a = true ;
return 0;
}
|
Java Code:
class A
{
public static void main(String args[])
{
boolean a = true ;
}
}
|
- Default Value: Default value is the value initially stored in the variable, when it is declared, but not initialized to any value. The default value of boolean data type in Java is false, whereas in C++, it has no default value and contains garbage value (only in case of global variables, it will have default value as false).
C++ Code:
#include<iostream>
using namespace std;
int main()
{
bool a[5];
int i;
for (i=0; i<5; ++i)
{
cout << a[i] << " " ;
}
return 0;
}
|
All the values stored in the array in the above program are garbage values and are not fixed.
Java Code:
class A
{
public static void main(String args[])
{
boolean a[];
a = new boolean [ 5 ];
int i;
for (i= 0 ; i< 5 ; ++i)
{
System.out.println(a[i]);
}
}
}
|
Output:
false
false
false
false
false
All the values in the array a will be false by default, as illustrated in the above output.
- Use in Mathematical Expressions: One important difference is that boolean data type variables cannot be used in mathematical expressions in java as it will give an error, whereas they can be used easily so in C++.
The reason for this behaviour is that boolean variables are not converted into integer values (0 or 1) in Java, so they cannot be used so.
C++ Code:
#include<iostream>
using namespace std;
int main()
{
int a;
bool b = true ;
bool c = false ;
a = b + c;
cout << a;
return 0;
}
|
Output:
1
The output will be 1 as true will be converted to value 1 and false will be converted to value 0, so a will store 1, whereas the same code will give an error in java, as shown below-
Java Code:
class A
{
public static void main(String args[])
{
int a;
boolean b = true ;
boolean c = false ;
a = b + c;
System.out.println(a);
}
}
|
- Use with Relational Operators: In Java, boolean variables cannot be used with the relational operators like <, >, <=, and >= , whereas in C++, they can be used in this manner . However, they can be used with == and != operators in both Java and C++ .
This can be accounted to the fact that relational operators operate on numeric values and boolean variables are not stored as numeric values in Java, but are stored so in C++ (false is stored as 0 and true as 1) .
C++ Code:
#include<iostream>
using namespace std;
int main()
{
bool a = true ;
bool b = false ;
if (a > b)
{
cout << "a is greater than b" ;
}
else
{
cout << "a is smaller than b" ;
}
return 0;
}
|
Output:
a is greater than b
Java Code:
class a
{
public static void main(String args[])
{
boolean a = true ;
boolean b = false ;
if (a > b)
{
System.out.println( "a is greater than b" );
}
else
{
System.out.println( "a is smaller than b" );
}
}
}
|
- Floating point value: In C++, floating, integer values can be easily assigned to a boolean variable, as they will be implicitly type-casted into boolean, whereas doing so in Java will result in an error.
C++ Code:
#include<iostream>
using namespace std;
int main()
{
bool a = 7;
bool b = 7.0;
cout << a << " " << b;
return 0;
}
|
Output:
1 1
The above output results as storing any value in a boolean variable other than 0, will result in 1 being stored in that variable.
Java Code:
class a
{
public static void main(String args[])
{
boolean a = 7 ;
boolean b = 7.0 ;
System.out.println(a);
System.out.println(b);
}
}
|
- The size of boolean data type in C++ is 1 byte, whereas size of boolean in Java is not precisely defined and it depends upon the Java Virtual Machine (JVM).
Boolean values in Java always take more than one byte, but how much more depends where the value is being stored – in the stack, or on the heap. The JVM uses a 32-bit stack cell, which will cause each boolean value to occupy the complete stack cell of 32 bits. However, the size of boolean values on the heap are implementation dependent.
Last Updated :
12 Jun, 2017
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...