续《JAVA编程思想》学习备忘(第93页:Operators--1)
Short-circuiting
使用逻辑运算符时的“短路”现象:在并列逻辑判断项运算过程中得到的值如可明确为最终结果,其后的逻辑运算将不执行。
举个例子:
import static staticPrint.Print.print;
public class ShortCircuit{
static boolean test1(int val){
print("test1(" + val + ")");
print("result: " + (val < 1));
return val < 1;
}
static boolean test2(int val){
print("test2(" + val + ")");
print("result: " + (val < 2));
return val < 2;
}
static boolean test3(int val){
print("test3(" + val + ")");
print("result: " + (val < 3));
return val < 3;
}
public static void main(String[] args){
boolean b = test1(0) && test2(2) && test3(2);
print("expression is " + b);
}
}
打印结果为:
test1(0) result: true test2(2) result: false expression is false
可以清楚地看到 test3(2) 没被执行。test1(0) 返回了true,test2(2)返回了false,正是当test2(2)返回结果时,可决定整个逻辑判断表达式最终的结果,所以下边的逻辑运算随即终止。这种现象就叫“短路”。
Literals
文字描述的值:
import static staticPrint.Print.print;
public class Literals{
public static void main(String[] args){
int i1 = 0x2f; //Hexadecimal(lowercase)
print("i1: " + Integer.toBinaryString(i1));
int i2 = 0X2F; //Hexadecimal(uppercase)
print("i2: " + Integer.toBinaryString(i2));
int i3 = 0177; //Octal(leading zero)
print("i3: " + Integer.toBinaryString(i3));
char c = 0xffff; //max char hex value
print("c: " + Integer.toBinaryString(c));
byte b = 0x7f; //max byte hex value
print("b: " + Integer.toBinaryString(b));
short s = 0x7fff; //max short hex value
print("s: " + Integer.toBinaryString(s));
long n1 = 200L; //long suffix
long n2 = 200l; //long suffix(but can be confusing)
long n3 = 200;
float f1 = 1;
float f2 = 1F; //float suffix
float f3 = 1f; //float suffix
double d1 = 1d; //double suffix
double d2 = 1D; //double suffix
//(Hex and Octal also work with long)
}
}
Exponential notation
指数记法:
import static staticPrint.Print.print;
public class Exponents{
public static void main(String[] args){
//Uppercase and lowercase 'e' are the same:
float expFloat = 1.39e-43f;
expFloat = 1.39E-43f;
print(expFloat);
double expDouble = 47e47d; // 'd' is optional
double expDouble2 = 47e47; // Automatically double
print("/n"+expDouble);
}
}
输出结果:1.39E-43 4.7E48
1.39E-43表示1.39乘以10的-43次方。
没有理解为什么会输出4.7E48 而不是47E47(虽然值一样)希望看到这个问题的网友能帮助解答一下。
Bitwise operators
按位运算符
(本小节略)
Shift operators
移位运算符
(本小节略)
Ternary if-else operator
三元运算符,也称作条件运算符。
在使用时要小心,它容易产生不能读取的代码。
它不同与if-else,因为它产生一个值。举例进行两者的比较:
import static staticPrint.Print.print;
public class TernaryIfElse{
static int ternary(int i){
return i < 10 ? i * 100 : i*10;
}
static int standardIfElse(int i){
if(i < 10)
return i * 100;
else
return i * 10;
}
public static void main(String[] args){
print(ternary(9));
print(" "+ternary(10));
print(" "+standardIfElse(9));
print(" "+standardIfElse(10));
}
}
输出结果:900 100 900 100
String operator + and +=
字符串运算符有着一些有趣的行为。如果一个表达式以String开头,那么所有跟随的运算对象必须为Strings 。
举例:
import static staticPrint.Print.print;
public class StringOperators{
public static void main(String[] args){
int x = 0, y = 1, z = 2;
String s = "x, y, z ";
print(s + x + y + z + "/n");
print(x + " " + s + "/n"); //Converts x to a String
s += "(summed) = "; //Concatenation operator
print(s + (x + y + z) + "/n");
print(" " + x); //Shorthand for Integer.toString();
}
}
输出结果:x, y, z 012
0 x, y, z
x, y, z (summed) = 3
0
注间以上程序括号的使用:print(s + (x + y + z));先把括号中的值求出,再转为字符串。
(待续)