那些年踩过的坑

Java基础方面陷阱

其实这些坑,很大程度在于一些细节,或者是对基本的语法不是很了解

什么是陷阱

  • 简洁的定义:陷阱,是指那些能够正常编译,但是在执行时却产生事与愿违的,有时候甚至是灾难性后果的程序代码。
  • 广义的定义:任何可能导致程序员把大量的时间浪费在开发工具的使用上而不是最终软件的进展上的语言特性、API或系统,都可以称呼为陷阱。

如何分析

  • 首先找到是哪一个代码造成的问题,陷阱的类型是什么。
  • 问题的根源
    • 这个是揭示陷阱最重要的一个部分,我们要深入底层,了解可能导致程序员绊脚的详细内部工作过程、无效的假设或者API的缺陷。
  • 解决方案
    • 这个是分析陷阱的最后一个步骤,最终给出一个程序实现和运行结果。

例1:找奇数

1
2
3
4
5
6
7
8
9
10

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));
}

想一下这个条件:i % 2 != 1;当把1、2、3输入进去时,结果好像也对哦,但是就是少了一方面。
所以答案应该是:i % 2 != 0,正负数都是可以用的了

例2:浮点数相减

1
2
3
public static void main(String[] args) {
System.out.println(2.0 - 1.1);
}

这个2.0 - 1.1,一开始我以为结果是0.9的,结果一运行才发现0.8999999999999999
应该改为:

1
2
System.out.println(new BigDecimal("2.0").subtract(new BigDecimal("1.1")));
System.out.printf("%.1f", 2.0-1.1);

例3:长整除

1
2
3
4
5
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);
}

这个我们看起来可以约分似的,以为结果肯定就是1000了,然而并非是这样。。
要换成:

1
2
final long MICROS_PER_DAY = 24L * 60 * 60 * 1000 * 1000;
final long MILLIS_PER_DAY = 24L * 60 * 60 * 1000;

例4:互换内容

1
2
3
4
5
6
public static void main(String[] args) {
int x = 1984;
int y = 2001;
x^= y^= x^= y;
System.out.println("x= " + x + "; y= " + y);
}

互相交换两位数,平时我们要加一个中间数,这里的话,不需要也可以,但是这道题。。
应该改为:y=(x^= (y^= x))^ y;

例5:字符串和字符

1
2
3
4
public static void main(String[] args) {
System.out.println("H" + "a"); //Ha
System.out.println('H' + 'a'); //169
}

这个字符与字符串两个之间..

例6:字符数组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public static void main(String[] args) {
String letters = "ABC";
char[] numbers = {'1', '2', '3'};
System.out.println(letters + " easy as " + numbers);
//结果是:ABC easy as [C@7852e922
}

```

结果是:ABC easy as 123 ???
改为:
```java
System.out.print(letters + " easy as " );
System.out.print(numbers);

例7:转义字符

1
2
3
4
public static void main(String[] args) {
System.out.println("a\u0022.length() +\u0022b".length());
System.out.println("a".length() +"b".length());
}

\u0022表示第34号的unicode字符 “ 双引号
运行结果:2 2

例8:打印输出类名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public static void main(String[] args) {
System.out.println(
MyClass.class.getName().
replaceAll("\\.","/") + ".class");
}
```


### 例9:随机数的问题
```java
private static Random rnd = new Random();
public static void main(String[] args) {
StringBuffer word = null;
switch(rnd.nextInt(3)) {
case 1: word = new StringBuffer("P");break;
case 2: word = new StringBuffer("G");break;
default: word = new StringBuffer("M");
}
word.append('a');
word.append('i');
word.append('n');
System.out.println(word);
}

运行结果:Main

例10:增量操作

1
2
3
4
5
6
7
public static void main(String[] args) {
int j = 0;
for (int i = 0; i < 100; i++){
j = j++;
}
System.out.println(j);
}

运行结果是:0

例11:整数边界的问题

1
2
3
4
5
6
7
8
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);
}

例12:计数器的问题

1
2
3
4
5
6
7
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);
}

例13:优柔寡断的返回值

1
2
3
4
5
6
7
8
9
10
public static void main(String[] args) {
System.out.println(decision());
}
public static boolean decision() {
try {
return true;
} finally {
return false;
}
}

例14:你好,再见

1
2
3
4
5
6
7
8
public static void main(String[] args) {
try {
System.out.println("Hello world");
System.exit(0);
} finally {
System.out.println("Goodbye world");
}
}

例15:到底关闭了吗

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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();
}
}

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 1210331079@qq.com

💰

Title:那些年踩过的坑

Count:1.2k

Author:千 羽

Created At:2020-05-19, 22:34:46

Updated At:2020-05-30, 10:43:09

Url:https://nateshao.github.io/2020/05/19/%E9%82%A3%E4%BA%9B%E5%B9%B4%E8%B8%A9%E8%BF%87%E7%9A%84%E5%9D%91/

Copyright: 'Attribution-non-commercial-shared in the same way 4.0' Reprint please keep the original link and author.

×

donation.headline

// 底部音乐
//右上角Github图标