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 // 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 // But this program prints false. class Geeksforgeeks { public static void main(
2 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 #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 results
1 min read
Comparison of Autoboxed Integer Objects in Java
In Java, autoboxing is a feature that automatically converts primitive types into their corresponding wrapper class objects. Autoboxing simplifies code and also eliminates the need for manual conversion between primitive types and wrapper objects.Note: Autoboxing happens when an object requires an o
3 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
Boolean Data Type
In programming languages, we have various data types to store different types of data. Some of the most used data types are integer, string, float, and boolean. The boolean data type is a type of data that stores only two types of values i.e. True or False. These values are not case-sensitive depend
13 min read
Boolean equals() method in Java with examples
The equals() method of Boolean class is a built in method of Java which is used check equality of two Boolean object. Syntax: BooleanObject.equals(Object ob) Parameter: It take a parameter ob of type Object as input which is the instance to be compared. Return Type: The return type is boolean. It re
2 min read