+ 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.
6 odpowiedzi
+ 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
+ 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?
+ 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 😂
+ 1
Jakko Jak thanks buddy, for the brief explaination.😇😇
+ 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.
0
Jakko Jak yeah😂 i got that