Comparison of boolean Data Type in C++ and Java
Last Updated :
21 Apr, 2025
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:
Java
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:
Java
class A {
public static void main(String args[]) {
boolean[] arr = new boolean[5];
for (boolean i : arr) {
System.out.println(i);
}
}
}
Outputfalse
false
false
false
false
3. Use in Mathematical Expressions: In Java, boolean values can not be added or used in arithmetic expressions.
Example:
Java
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:
Java
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:
Java
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:
C++
#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:
C++
#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:
C++
#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:
C++
#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;
}
Outputa is greater than b
5. Assigning int or float to bool: In C++, we can assign a number value to a boolean variable.
Example:
C++
#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;
}
6. Memory Size: C++ allocates 1byte for bool.
Similar Reads
Comparison of double and float primitive types in Java
Consider the following two codes in Java: Java Code // This program prints true class Geeksforgeeks { public static void main(String args[]) { float f = 5.25f; double d = 5.25 System.out.println(f == d); } } Output true Java Code // But this program prints false. class Geeksforgeeks { public static
2 min read
Comparison of Inheritance in C++ and Java
The purpose of inheritance is the same in C++ and Java. Inheritance is used in both languages for reusing code and/or creating an âis-aâ relationship. The following examples will demonstrate the differences between Java and C++ that provide support for inheritance. 1) In Java, all classes inherit fr
4 min read
Comparison of Exception Handling in C++ and Java
Both languages use to try, catch and throw keywords for exception handling, and their meaning is also the same in both languages. Following are the differences between Java and C++ exception handling: Java C++ Only throwable objects can be thrown as exceptions.All types can be thrown as exceptions.W
4 min read
Boolean compareTo() method in Java with examples
The compareTo() method of Boolean class is a built in method in Java which is used to compare the given Boolean instance with the current instance. Syntax: BooleanObject.compareTo(Boolean a) Parameters: It takes a Boolean value a as parameter which is to be compared with the current instance. Return
2 min read
Boolean compare() method in Java with Examples
The compare() method of Boolean class is a built in method in Java which is used to compare two boolean values. It is a static method, so it can be called without creating any object of the Boolean class i.e. directly using the class name. Syntax: Boolean.compare(boolean a, boolean b) Parameters: It
2 min read
Results of comparison operations in C and C++
In C, data type of result of comparison operations is int. For example, see the following program. C/C++ Code #include<stdio.h> int main() { int x = 10, y = 10; printf("%d \n", sizeof(x == y)); printf("%d \n", sizeof(x < y)); return 0; } Output4 4 Whereas in C++, type of
1 min read
Comparison of Autoboxed Integer objects in Java
When we assign an integer value to an Integer object, the value is autoboxed into an Integer object. For example the statement "Integer x = 10" creates an object 'x' with value 10. Following are some interesting output questions based on comparison of Autoboxed Integer objects. Predict the output of
2 min read
Java Guava | Booleans.compare() method with Examples
The compare() method of Booleans Class in the Guava library is used to compare the two specified boolean values. These values are passed as the parameter and the result of comparison is found as the difference of 1st value and the 2nd value. Hence it can be positive, zero or negative. Syntax: public
2 min read
Boolean hashCode() method in Java with examples
The hashCode() method of Boolean class is a built in method to return a int hashcode value corresponding to the Boolean object. Syntax BooleanObject.hashCode() Return Type: It returns a int hashcode value corresponding to the Boolean object. It returns 1231 if the Boolean object stores the value tru
1 min read
C++ Compound Data Types Quiz
Built-in data types cannot store all the information in an easily accessible and organized way. That is why C++ provides compound data types such as arrays, pointers, strings, etc. that are derived from the built-in data types and provide different way to use them. Good understanding of compound dat
2 min read