条件语句

Java 使用 boolean 布尔值变量作为条件的结果值 . boolean变量的值有 `true` and `false` 。他们作为一段语句 比较 或 运算 后的结果返回 ，如下

``````int a = 4;
boolean b = a == 4;
if (b) {
System.out.println("It's true!");
}
``````

``````int a = 4;
if (a == 4) {
System.out.println("Ohhh! So a is 4!");
}
``````

Boolean 布尔运算符

``````int a = 4;
int b = 5;
boolean result;
result = a < b; // true
result = a > b; // false
result = a <= 4 // a smaller or equal to 4 - true
result = b >= 6 // b bigger or equal to 6 - false
result = a == b // a equal to b - false
result = a != b // a 不等于 b - true
result = a > b || a < b // 逻辑或 or - true
result = 3 < a && a < 6 // 逻辑与 and - true
result = !result // 逻辑非 not - false
``````

if - else 和 between

IF 语句非常简单.

``````if (a == b) {
// a and b are equal, let's do something cool
}
``````

``````if (a == b) {
// We already know this part
} else {
// a and b are not equal... :/
}
``````

``````if (a == b) System.out.println("Yeah!");
else System.out.println("Ohhh...");
``````

``````if (a == b)
System.out.println("Another line Wow!");
else
System.out.println("Double rainbow!");
``````

还有一种丑陋的方式替代if

``````int a = 4;
int result = a == 4 ? 1 : 8;
// result will be 1
// This is equivalent to
int result;
if (a == 4) {
result = 1;
} else {
result = 8;
}
``````

== 和 equals

``````String a = new String("Wow");
String b = new String("Wow");
String sameA = a;

boolean r1 = a == b;      // 返回false, 因为ab不是同一个对象
boolean r2 = a.equals(b); // true, ab的内容是一样的
boolean r3 = a == sameA;  // true, a 和 sameA 就是同一个对象
``````