# Common trap questions and answers in Java

- 2020-05-30 20:07:15
- OfStack

1. Look for odd Numbers:

```
public static boolean isOdd(int i){
return i % 2 == 1;
}
```

Can the above method really find all the odd Numbers?

A: negative Numbers are not considered. If i is negative, it is not true. return i%2 == 0

2. Floating point subtraction

```
System.out.println(2.0-1.9);
```

A: the simple floating point types float and double in Java cannot be operated on. This is true not only for Java, but for many other programming languages as well. In most cases, the results are accurate, but a few more tries (one loop) can yield errors like the one above. Of course, you can have problems with addition, subtraction, multiplication and division,

Such as:

```
System.out.println(0.05+0.01);
System.out.println(1.0-0.42);
System.out.println(4.015*100);
System.out.println(123.3/100);
```

This is because some decimal Numbers with a finite number of decimal digits in decimal 10 May become infinitely repeating decimal Numbers in decimal 2, which can not be represented in floating point Numbers and damage the accuracy.

Solutions:

1. If you want to determine whether a-b is equal to c, or a+b is equal to c, you can use it

```
if(0.05+0.01-0.06 < 0.0000001)
{
}
```

2. In the book Effective Java, there is a principle that float and double can only be used for scientific or engineering calculations, but we use them in business calculations
`java.math.BigDecimal`

To solve the

```
System.out.println((new BigDecimal("2.0")).subtract(
new BigDecimal("1.9")).doubleValue());
```

3. Infinite loop

```
public static final int END = Integer.MAX_VALUE;
public static final int START = END - 2;
public static void main(String[] args) {
int count = 0;
for (int i = START; i <= END; i++)
count++;
System.out.println(count);
}
```

A: the reason for the infinite loop here is that when i is Integer.MAX_VALUE, for loop is first ++, and then i
<
=END, when i is Integer.MAX_VALUE ++, i becomes a negative number. So I'm just going to keep going.

The reason it's negative is because int overflowed. There will be
<
= END into
<
END solves the problem.

4. What does it return?

```
public static boolean decision() {
try {
return true;
} finally {
return false;
}
}
```

A: return false. At this time
`return true`

Is an unreachable statement, and optimizations are removed at compile time.

3. Here's a possible interview trap

Look at the code:

```
int a=5;
System.out.println("value is"+((a<5)? 10.9:9 ));
```

The output result is:

B10.9 C. 9 D none of the answers above are correct.

The execution result is as follows:

```
value is9.0
```

Because ((a
<
5) & # 63; 10.9) one 10.9java is automatically transformed according to the operator precision. So this 9 is going to be 9.0.

So D.

a

```
StringBuffer str1=new StringBuffer("123");
StringBuffer str2=new StringBuffer("123");
if(str1.equals(str2)){
System.out.println("str1.equalstr2");
}else{
System.out.println("str1.notequalstr2");
}
```

The result is:
`str1.notequalsstr2 `

This means that StringBuffer does not override the equals method.

```
System.out.println(2.0-1.9);
```

0
The result is:

```
System.out.println(2.0-1.9);
```

1
If there are other welcome additions.

Reference:

1. http://blog.csdn.net/ol_beta/article/details/5598867

2. http://zhidao.baidu.com/link?url=0UyDU42L7DXZitdydJMG3IIUDIf3xidFCRAObZAq6SHFCEaNnp2Oyuq1KVwBvmlR0UZGHSjD4f6A1yD0d65JL_

3. http://bbs.csdn.net/topics/300023952

4. http://z466459262.iteye.com/blog/739300

conclusion

The above is the whole content of this article, I hope the content of this article to your study or work can bring 1 definite help, if you have questions you can leave a message to communicate.