什么是陷阱?
简洁的定义:
陷阱,是指那些能够正常编译,但是在执行时却产生事与愿违的,有时候甚至是灾难性后果的程序代码。
广义的定义:
任何可能导致程序员把大量的时间浪费在开发工具的使用上而不是最终软件的进展上的语言特性、API或系统,都可以称呼为陷阱。
陷阱的分类?
分析陷阱三重奏?
症状或者问题:
首先找到是哪一个代码造成的问题,陷阱的类型是什么。
问题的根源:
这个是揭示陷阱最重要的一个部分,我们要深入底层,了解可能导致程序员绊脚的详细内部工作过程、无效的假设或者API的缺陷。
解决方案:
这个是分析陷阱的最后一个步骤,最终给出一个程序实现和运行结果。
NO.1 找奇数
public class OddTest {
public static boolean isOdd(int i){
return i % 2 == 1;
}
public static void main(String[] args) {
System.out.println(isOdd(1));
System.out.println(isOdd(2));
System.out.println(isOdd(3));
}
}
陷阱等级:★★★☆☆
NO.2 浮点数相减
public class DoubleMinus {
public static void main(String[] args) {
System.out.printf(2.0-1.1);
}
}
陷阱等级:★★★★★
NO.3 长整除
public class LongDivision {
public static void main(String[] args) {
final long MICROS_PER_DAY = 24 * 60 * 60 * 1000 * 1000; //微秒
final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000; //毫秒
System.out.println(MICROS_PER_DAY/MILLIS_PER_DAY);
}
}
陷阱等级:★★★★★
NO.4 互换内容
public class Swap {
public static void main(String[] args) {
int x = 1984;
int y = 2001;
x^= y^= x^=y;
System.out.println("x= " + x + "; y= " + y);
}
}
陷阱等级:★★☆☆☆
NO.5 字符串和字符
public class CharAndString {
public static void main(String[] args) {
System.out.println("H" + "a");
System.out.println('H' + 'a');
}
}
陷阱等级:★★★★★
NO.6 字符数组
public class CharArray {
public static void main(String[] args) {
String letters = "ABC";
char[] numbers = {'1', '2', '3'};
System.out.print(numbers);
System.out.print(letters + " easy as "+numbers);
}
}
陷阱等级:★★★★☆
NO.7 转义字符
public class UnicodeTest {
public static void main(String[] args) {
System.out.println("a\u0022.length() +\u0022b".length());
}
}
陷阱等级:★★☆☆☆
NO.8 打印输出类名
public class MyClass {
public static void main(String[] args) {
System.out.println(MyClass.class.getName().replaceAll(".","/") + ".class");
}
}
陷阱等级:★★☆☆☆
NO.9 随机数的问题
import java.util.Random;
public class RandomTest {
private static Random rnd = new Random();
public static void main(String[] args) {
StringBuffer word = null;
switch(rnd.nextInt(2)) {
case 1: word = new StringBuffer("P");
case 2: word = new StringBuffer("G");
default: word = new StringBuffer("M");
}
word.append('a');
word.append('i');
word.append('n');
System.out.println(word);
}
}
陷阱等级:★★★★☆
NO.10 无情的增量操作
public class ForTest {
public static void main(String[] args) {
int j = 0;
for (int i = 0; i < 100; i++){
j = j++;
}
System.out.println(j);
}
}
陷阱等级:★★★★★
NO.11 整数边界的问题
public class WhileTest {
public static final int END = Integer.MAX_VALUE;
public static final int START = END - 100;
public static void main(String[] args) {
int count = 0;
for (int i = START; i <= END; i++)
count++;
System.out.println(count);
}
}
陷阱等级:★★★★☆
NO.12 计数器的问题
public class Clock {
public static void main(String[] args) {
int minutes = 0;
for (int ms = 0; ms < 60*60*1000; ms++)
if (ms % 60*1000 == 0)
minutes++;
System.out.println(minutes);
}
}
陷阱等级:★★★★☆
NO.13 优柔寡断的返回值
public class ReturnValue {
public static void main(String[] args) {
System.out.println(decision());
}
public static boolean decision() {
try {
return true;
} finally {
return false;
}
}
}
陷阱等级:★★★★☆
NO.14 你好,再见
public class GoodBye {
public static void main(String[] args) {
try {
System.out.println("Hello world");
System.exit(0);
} finally {
System.out.println("Goodbye world");
}
}
}
陷阱等级:★★★★☆
NO.15 到底关闭了吗
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class IOTest {
public static void main(String[] args) {
}
public static void copy(String src, String dest) throws IOException {
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(src);
out = new FileOutputStream(dest);
byte[] buf = new byte[1024];
int n;
while ((n = in.read(buf)) > 0)
out.write(buf, 0, n);
} finally {
if (in != null) in.close();
if (out != null) out.close();
}
}
}
陷阱等级:★★★★★
整理自 www.ibeifeng.com