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


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:


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.


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++)

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.


StringBuffer str1=new StringBuffer("123"); 
  StringBuffer str2=new StringBuffer("123"); 

The result is: str1.notequalsstr2  This means that StringBuffer does not override the equals method.


The result is:


If there are other welcome additions.







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.

Related articles: