指数表示法(Exponential notation)
使用e或E跟随一个整数表示幂指数,如果是负值,则需要跟f或d表示小数;如果为整数则跟L表示long,不跟则表示为int。下面看一个例子1-1 指数表示法的例子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;
System.out.println(expFloat);
double expDouble = 47e47d; // 'd' is optional
double expDouble2 = 47e47; // Automatically double
System.out.println(expDouble);
}
}
程序运行的结果如下:
注意:在科学工程中,‘e’指的是自然对数,它的值接近于2.718(在Java语言中用Math.E来表示),但是在代码1-1 中,e指的的‘10的次方’,这点在Java编程语言中应该注意。逻辑运算符(Logical operators)
逻辑运算符包括:与(&&)、或(||)、非(!)1-2 逻辑运算符的例子public class Bool {
public static void main(String[] args) {
Random rand = new Random(47);
int i = rand.nextInt(100);
int j = rand.nextInt(100);
System.out.println("i = " + i);
System.out.println("j = " + j);
System.out.println("i > j is " + (i > j));
System.out.println("i < j is " + (i < j));
System.out.println("i >= j is " + (i >= j));
System.out.println("i <= j is " + (i <= j));
System.out.println("i == j is " + (i == j));
System.out.println("i != j is " + (i != j));
// Treating an int as a boolean is not legal Java:
//! System.out.println("i && j is " + (i && j));
//! System.out.println("i || j is " + (i || j));
//! System.out.println("!i is " + !i);
System.out.println("(i < 10) && (j < 10) is "
+ ((i < 10) && (j < 10)) );
System.out.println("(i < 10) || (j < 10) is "
+ ((i < 10) || (j < 10)) );
}
}
程序运行的而结果如下:
位操作符(Bitwise operators)
位操作符主要有以下几种:& (按位与)
| (按位或)
∧ (按位非)
~ (取反)
注意:前三种位操作符可以与“=”结合使用,但是“~”是单目运算符,不能与“=”结合使用。另外位操作符的“&”只是对两个而进制数进行按与操作,得到一个二进制的结果,不具有“短路”的功能。而逻辑运算符“&&”具有“短路”的功能,当第一个条件为假时,便不再对后面的条件进行判断。
注:文章的代码摘自 Thinking in Java(Fourth Edition)英文版,作者 [美]Bruce Eckef,刘中兵 评注。