table of Contents
Quote
The conversion of true, false and 1, 0 in the programming language
C ++ true and false code demonstration
True, false and 1, 0 conversion principle
Boolean is converted to the number
false is 0, true is 1 the
number is converted to Boolean
0 is false; non-zero is true
Java does not support direct forced transfer
1. Convert Boolean to number-false is 0, true is 1
The only way: Sanmu statement
int myInt = myBoolean ? 1 : 0;
Sample code:
boolean myBoolean = true;
int myInt = myBoolean ? 1 : 0;
System.out.println(myInt); //输出1
myBoolean = false;
myInt = myBoolean ? 1 : 0;
System.out.println(myInt); //输出0
Second, the number is converted to Boolean-0 is false; non-zero is true
method one:
boolean myBoolean = myInt != 0;
Sample code:
int myInt= 2;
boolean myBoolean = myInt!= 0;
System.out.println(myBoolean); //输出为true
myInt= 0;
myBoolean = myInt!= 0;
System.out.println(myBoolean); //输出为false
Method 2: Trinocular statement
int a = 1; //带转化int整数
boolean result = (a==0)?false:true; //转化语句
Sample code:
int myInt = 2; //被转化int整数
boolean myBoolean = (myInt == 0) ? false : true; //转化语句
System.out.println(myBoolean); //输出为true
myInt = 0; //被转化int整数
myBoolean = (myInt == 0) ? false : true; //转化语句
System.out.println(myBoolean); //输出为true