//: operators/ShortCircuit.java
// Demonstrates short-circuiting behavior
// with logical operators.
//import static net.mindview.util.Print.*;public class ShortCircuit {
static boolean test1(int val) {
System.out.println("test1("+ val +")");
System.out.println("result: " + (val < 1));
return val < 1;
}
static boolean test2(int val) {
System.out.println("test2("+ val +")");
System.out.println("result: " + (val < 2));
return val < 2;
}
static boolean test3(int val) {
System.out.println("test3("+ val +")");
System.out.println("result: " + (val < 3));
return val < 3;
}
public static void main(String[] args) {
boolean b = test1(0) && test2(2) && test3(2);
System.out.println("expression is " + b);
}
} /* Output:
test1(0)
result: true
test2(2)
result: false
expression is false
*///:~
问题:("test1("+ val +")");
这里+ val +怎么理解?
// Demonstrates short-circuiting behavior
// with logical operators.
//import static net.mindview.util.Print.*;public class ShortCircuit {
static boolean test1(int val) {
System.out.println("test1("+ val +")");
System.out.println("result: " + (val < 1));
return val < 1;
}
static boolean test2(int val) {
System.out.println("test2("+ val +")");
System.out.println("result: " + (val < 2));
return val < 2;
}
static boolean test3(int val) {
System.out.println("test3("+ val +")");
System.out.println("result: " + (val < 3));
return val < 3;
}
public static void main(String[] args) {
boolean b = test1(0) && test2(2) && test3(2);
System.out.println("expression is " + b);
}
} /* Output:
test1(0)
result: true
test2(2)
result: false
expression is false
*///:~
问题:("test1("+ val +")");
这里+ val +怎么理解?
("test2("+ val +")"):这个应该是很清楚的吧,就是要打印出test2(*)这个字符串。但是这*呢是个变量,值是会改变的所以,你要想打印出这种效果来,就是把括号也引起来.而 "test2(" , Val , “)”都是作为三个字符串来出理。
如果var=3,则"test1("+ val +")" = "test1(3)"