17. 不要使用基于减法的比较器
- Comparator<Integer> c = new Comparator<Integer>() {
- public int compare(Integer i1, Integer i2) {
- return i1 - i2;// 升序
- }
- };
- List<Integer> l = new ArrayList<Integer>();
- (new Integer(-2000000000));
- (new Integer(2000000000));
- (l, c);
- (l);// [2000000000, -2000000000]
Comparator<Integer> c = new Comparator<Integer>() {
public int compare(Integer i1, Integer i2) {
return i1 - i2;// 升序
}
};
List<Integer> l = new ArrayList<Integer>();
(new Integer(-2000000000));
(new Integer(2000000000));
(l, c);
(l);// [2000000000, -2000000000]
上面程序的比较器是升序,结果却不是这样,比较时出现了什么问题?
先看看下面程序片断:
- int x = -2000000000;
- int y = 2000000000;
- /*
- * -2000000000 即 -(01110111001101011001010000000000)
- * 的补码为: 10001000110010100110110000000000
- *
- * 计算过程使用竖式表示:
- * 10001000110010100110110000000000
- * 10001000110010100110110000000000
- * --------------------------------
- * 00010001100101001101100000000000
- *
- * 计算结果溢出,结果为294967296
- */
- (x - y);// 294967296
int x = -2000000000;
int y = 2000000000;
/*
* -2000000000 即 -(01110111001101011001010000000000)
* 的补码为: 10001000110010100110110000000000
*
* 计算过程使用竖式表示:
* 10001000110010100110110000000000
* 10001000110010100110110000000000
* --------------------------------
* 00010001100101001101100000000000
*
* 计算结果溢出,结果为294967296
*/
(x - y);// 294967296
所以不要使用减法的比较器,除非能确保要比较的数值之间的距离永远不会大于Intger. MAX_VALUE。
基于整型的比较器的实现一般使用如下的方式来比较:
- public int compare(Integer i1, Integer i2) {
- return (i1 < i2 ? -1 : (i1 == i2 ? 0 : 1));
- }
public int compare(Integer i1, Integer i2) {
return (i1 < i2 ? -1 : (i1 == i2 ? 0 : 1));
}
18. int i=-2147483648与int i=-(2147483648)?
- int i=-(2147483648);
int i=-(2147483648);
编译通不过!为什么
int字面常量2147483638只能作为一元负操作符的操作数来使用。
类似的还有最大long:
- long i=–(9223372036854775808L);
long i=–(9223372036854775808L);
字符串
19. char类型相加
- ('a' + 'A');//162
('a' + 'A');//162
上面的结果不是 aA ,而是 162。
当且仅当+操作符的操作数中至少有一个是String类型时,才会执行字符串连接操作;否则,执行加法。如果要连接的
数值没有一个是字符串类型的,那么你可以有几种选择:预置一个空字符串("" + 'a' + 'A');将第一个数值用
()显示地转换成一个字符串(('a') + 'A');使用一个字符串缓冲区(
('a');('A'););或者如果使用的是JDK5.0,可以用printf(("%c%c",'a','A'));
20. 程序中的Unicode转义字符
- ///u0022是双引号的Unicode编码表示
- ("a/() + /u0022b".length());// 2
///u0022是双引号的Unicode编码表示
("a/() + /u0022b".length());// 2
Unicode编码表示的字符是在编译期间就转换成了普通字符,它与普通转义字符(如:/")是不一样的,它们是在程序
被解析为各种符号之后才处理转义字符序列。
21. 注释中的Unicode转义字符
如果程序中含有以下的注释:// d:/a/b/util ,程序将不能编译通过,原因是/u后面跟的不是四个十六进制数字,但
编译器在编译时却要把/u开头的字符的字符看作是Unicode编码表示的字符。
所以要注意:注释中也是支持Unicode转义字符的。
另外一个问题是不能在注释行的中间含有 /u000A 表示换行的Unicode字符,因为这样在编译时读到 /u000A 时,表示
行结束,那么后面的字符就会当作程序代码而不在是注释了。
22. Windows与Linux上的行结束标示符
- String line = (String)().get("");
- for(int i =0; i < ();i++){
- ((int)(i));
- }
String line = (String)().get("");
for(int i =0; i < ();i++){
((int)(i));
}
在Windows上运行结果:
13
10
在Linux上运行的结果:
10
在Windows平台上,行分隔符是由回车(/r)和紧其后的换行(/n)组成,但在Unix平台上通常使用单独的换行(/n)
表示。
23. 输出0-255之间的ISO8859-1符
- byte bts[] = new byte[256];
- for (int i = 0; i < 256; i++) {
- bts[i] = (byte) i;
- }
- // String str = new String(bts,"ISO8859-1");//正确的做法
- String str = new String(bts);//使用操作系统默认编码方式编码(XP GBK)
- for (int i = 0, n = (); i < n; i++) {
- ((int) (i) + " ");
- }
byte bts[] = new byte[256];
for (int i = 0; i < 256; i++) {
bts[i] = (byte) i;
}
// String str = new String(bts,"ISO8859-1");//正确的做法
String str = new String(bts);//使用操作系统默认编码方式编码(XP GBK)
for (int i = 0, n = (); i < n; i++) {
((int) (i) + " ");
}
上面不会输出0-255之间的数字串,正确的方式要使用new String(bts," ISO8859-1") 方式来解码。
ISO8859-1是唯一能够让该程序按顺序打印从0到255的整数的缺少字符集,这也是唯一在字符和字节之间一对一的映射
字符集。
通过java获取操作系统的默认编码方式:
- ("");//jdk1.4或之前版本
- ();//jdk1.5或之后版本
("");//jdk1.4或之前版本
();//jdk1.5或之后版本
24. String的replace()与replaceAll()
- (".".replaceAll(".class", "//$"));
(".".replaceAll(".class", "//$"));
上面程序将 . 替换成 /$,但运行时报异常,主要原replaceAll的第二参数有两个字符(/ $)是特殊字符,具有特殊
意思(/用来转移 / 与 $,$后面接数字表示反向引用)。另外,replaceAll的第一参数是正则表达式,所以要注意特
殊字符,正确的作法有以下三种:
- (".class".replaceAll("//.", "//$"));
- (".class".replaceAll("//Q.//E", "//$"));
- (".class".replaceAll(("."), ("//$")));
(".class".replaceAll("//.", "//$"));
(".class".replaceAll("//Q.//E", "//$"));
(".class".replaceAll(("."), ("//$")));
API对/、/Q与/E的解释:
/ 引用(转义)下一个字符
/Q引用所有字符,直到 /E
/E结束从 /Q 开始的引用
JDK5.0新增了一些解决此问题的新方法:
(String s):使用/Q与/E将参数引起来,这些被引用的字符串就是一般的字符,哪怕
含有正则式特殊字符。
(String s):将/与$转换成能应用于replaceAll第二个参数的字符串,
即可作为替换内容。
String的replace(char oldChar, char newChar)方法却不使用正则式,但它们只支持字符,而不是字符串,使用起来
受限制:
- (".".replace('.','//'));//能将 . 替换成 /
- (".".replace('.','$')); //能将 . 替换成 $
(".".replace('.','//'));//能将 . 替换成 /
(".".replace('.','$')); //能将 . 替换成 $
25. 一段程序的三个Bug
- Random rnd = new Random();
- StringBuffer word = null;
- switch ((2)) {
- case 1:
- word = new StringBuffer('P');
- case 2:
- word = new StringBuffer('G');
- default:
- word = new StringBuffer('M');
- }
- ('a');
- ('i');
- ('n');
- (word);
Random rnd = new Random();
StringBuffer word = null;
switch ((2)) {
case 1:
word = new StringBuffer('P');
case 2:
word = new StringBuffer('G');
default:
word = new StringBuffer('M');
}
('a');
('i');
('n');
(word);
上面的程序目的是等概率的打印 Pain、Gain、Main 三个单词,但多次运行程序却发现永远只会打印 ain,这是为什
么?
第一个问题在于:(2)只会返回0、1 两个数字,所以上面只会走case 1: 的分支语句,case 2: 按理是永
远不会走的。
第二个问题在于:如果case语句不以break结束时,则一直会往向运行,即直到执行到break的case语句止,所以上面
的的语句每次都会执行default分支语句。
第三个问题在于:StringBuffer的构造函数有两种可接受参数的,一个是StringBuffer(int capacity)、另一个是
StringBuffer(String str),上面用的是StringBuffer(char)构造函数,实质上运行时将字符型转换成了int型,这样
将字符当作StringBuffer的初始容量了,而不是字符本身。
以下是修改后的程序片段:
- Random rnd = new Random();
- StringBuffer word = null;
- switch ((3)) {
- case 1:
- word = new StringBuffer("P");
- break;
- case 2:
- word = new StringBuffer("G");
- break;
- default:
- word = new StringBuffer("M");
- break;// 可以不要
- }
- ('a');
- ('i');
- ('n');
- (word);
Random rnd = new Random();
StringBuffer word = null;
switch ((3)) {
case 1:
word = new StringBuffer("P");
break;
case 2:
word = new StringBuffer("G");
break;
default:
word = new StringBuffer("M");
break;// 可以不要
}
('a');
('i');
('n');
(word);