Is bool in java and that of c++ are different? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Is bool in java and that of c++ are different?

Hey anyone notice in java for a boolean variable, let say x and then using it in a conditional statement x=0 is a error. This is for all the C/C++ friends.

22nd Jul 2021, 2:07 PM
Ajay Varshney
Ajay Varshney - avatar
6 Answers
+ 2
In C++, since it doesn't actually have a boolean type, you can use 1 and 0 to represent true/false, and you can use true/false as conditions for it. Example: https://code.sololearn.com/ca5A9a1248a2 In Java, it considers 1 and 0 to be of the type int, and since it has a boolean type, you're bound to using true/false for it. If you tried to use 1/0 then it'll produce an error because it considers it an int instead of a bool. Example: https://code.sololearn.com/cA12a24A23A2
22nd Jul 2021, 3:10 PM
Jakko Jak
Jakko Jak - avatar
+ 1
As far I know its the same, a bool in any language is either true(1) or false(0). Can you elaborate more on what you really meant?
22nd Jul 2021, 3:04 PM
Rohit
+ 1
The Last Cherry Blossom i mean try yourself once, i know the real answer, i am just increasing my badge collection. Try this code in java and c++ both : bool x; if(x==1) PRINT "True" \\obiviously for java use \\system.out.println() \\ for c++ use cout << In the end you will find out both are different. In java for true and false, both has seperate definition other than number ones. PS: Don't forget to upvote this 😂
22nd Jul 2021, 3:10 PM
Ajay Varshney
Ajay Varshney - avatar
+ 1
Jakko Jak thanks buddy, for the brief explaination.😇😇
22nd Jul 2021, 3:13 PM
Ajay Varshney
Ajay Varshney - avatar
+ 1
lol No problem bro. Good luck on your badge. This was a good point to bring up and probably one that many people didn't realize.
22nd Jul 2021, 3:31 PM
Jakko Jak
Jakko Jak - avatar
0
Jakko Jak yeah😂 i got that
22nd Jul 2021, 3:36 PM
Ajay Varshney
Ajay Varshney - avatar