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. The main difference between them is listed below:
- In Java, the boolean type can hold false or true.
- In C++, the bool type can automatically convert between true/false and 1/0.
Boolean In C++ vs Java
The table below demonstrates the difference
Feature | C++(bool) | Java(boolean) |
|---|---|---|
Default values | The default value of C++ is false(0) | The default value of Java is false |
Accepted Values | In C++, any non-zero value is true, and the zero value is false | In Java, only true or false |
Type Conversion Support | It can convert integers to bool and vice versa. | In Java, there is no implicit conversion between boolean and int. |
Output Behavior | It prints 1 for true and 0 for false using std::cout | It prints true or false using System.out.println |
Strict Typing | It allows conversion between types | No automatic type conversion allowed |
Standard keyword | bool | boolean |
Java and boolean Data Type
1. Declaration: The declaration in Java is done by the keyword boolean
Syntax:
boolean flag = true;
Example:
class A
{
public static void main(String args[])
{
boolean a = true;
}
}
2. Default Values: 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.
Example:
class A {
public static void main(String args[]) {
boolean[] arr = new boolean[5];
for (boolean i : arr) {
System.out.println(i);
}
}
}
Output
false false false false false
3. Use in Mathematical Expressions: In Java, boolean values can not be added or used in arithmetic expressions.
Example:
int a;
boolean b = true, c = false;
// a = b + c; // Compile-time error
4. Use with Relational Operators: In Java, boolean variables cannot be used with the relational operators like <, >, <=, and >=
Example:
class A
{
public static void main(String args[])
{
boolean a = true;
boolean b = false;
//The following line will give an error
if (a > b)
{
System.out.println("a is greater than b");
}
else
{
System.out.println("a is smaller than b");
}
}
}
5. Assigning int or float to boolean: In Java, we can not assign a number value to a boolean variable.
Example:
class A
{
public static void main(String args[])
{
// invalid assignment in Java
boolean a = 7;
// invalid assignment in Java
boolean b = 7.0;
System.out.println(a);
System.out.println(b);
}
}
6. Memory Size: Java does not define a fixed size for boolean values; it depends on the JVM implementation.
C++ and Boolean Data Type
1. Declaration: The declaration in Java is done by keyword bool.
Syntax:
bool a = true;
Example:
#include<iostream>
using namespace std;
int main() {
bool a = true;
return 0;
}
2. Default Values: In C++, it has no default value and contains garbage value (only in case of global variables, it will have default value as false).
Example:
#include<iostream>
using namespace std;
int main()
{
// Declaring a boolean type array in C++
bool a[5];
int i;
for (i=0; i<5; ++i)
{
cout << a[i] << " ";
}
return 0;
}
3. Use in Mathematical Expressions: In C++ boolean variables can be used with Mathematical Expression.
Example:
#include<iostream>
using namespace std;
int main()
{
int a;
bool b = true;
bool c = false;
a = b + c;
cout << a;
return 0;
}
4. Use with Relational Operators: In C++ boolean variables can be used with relational operators.
Example:
#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
5. Assigning int or float to bool: In C++, we can assign a number value to a boolean variable.
Example:
#include<iostream>
using namespace std;
int main() {
// true, because non-zero integer is treated as true
bool a = 7;
// false, because zero is treated as false
bool b = 0.0;
// Output the values of a and b
cout << "a: " << a << endl;
cout << "b: " << b << endl;
return 0;
}
Output
a: 1 b: 0
6. Memory Size: C++ allocates 1byte for bool.