Home >Java >javaTutorial >Common trap questions and answers in Java
1. Find odd numbers:
public static boolean isOdd(int i){ return i % 2 == 1; }
Can the above method really find all odd numbers?
A: The problem of negative numbers is not taken into account. If i is negative, it is incorrect. Should 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. Not only Java, but also many other programming languages have this problem. In most cases, the calculated results are accurate, but if you try a few times (you can make a loop), you can make errors like the above. Of course, there may be problems with addition, subtraction, multiplication and division.
For example:
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 decimals with limited digits may become infinitely looping decimals in binary. In floating point numbers, cannot be represented and the accuracy is impaired.
Solution:
1. If you want to determine whether a-b is equal to c, or whether a+b is equal to c, you can use
if(0.05+0.01-0.06 < 0.0000001) { }
2. In "Effective Java" This book mentions a principle that float and double can only be used for scientific calculations or engineering calculations. In commercial calculations, we have to use java.math.BigDecimal to solve
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, the for loop first ++, then determines whether i is <=END, when i is Integer.MAX_VALUE and then ++ , i becomes a negative number. So the cycle continues. 4. What exactly is returned? A: Return false. At this time, return true is an unreachable statement and will be optimized and removed during the compilation phase. 3. Let’s share a trap question that you may encounter during the interview Look at the code: The output result is: A .Compilation error B10.9 C.9 D None of the above answers are correct. The execution result is: Because ((a<5) ? 10.9) has a 10.9java automatic transformation based on operator precision. Therefore, the following 9 will also become 9.0. So choose D. a The result is: str1.notequalsstr2 This shows that StringBuffer does not override the equals method. The result is: If you have anything else, welcome to add. Reference: 1. http://blog.csdn.net/ol_beta/article/details/5598867 2. http://zhidao. baidu.com/link?url=0UyDU42L7DXZitdydJMG3IIUDIf3xidFCRAObZAq6SHFCEaNnp2Oyuq1KVwBvmlR0UZGHSjD4f6A1yD0d65JL_
The reason why it becomes a negative number is that int overflows. Here, changing <=END to public static boolean decision() {
try {
return true;
} finally {
return false;
}
}
int a=5;
System.out.println("value is"+((a<5)? 10.9:9 ));
value is9.0
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");
}
Float fa=new Float(0.9f);
Float fb=new Float(0.9f); //Float fb=new Float("0.9f");
Double db=new Double(0.9f);
if(fa==fb){ //false
System.out.println("fa==fb");
}else{
System.out.println("fa!=fb");
}
if(fa.equals(fb)){ //true
System.out.println("fa.equalfb");
}else{
System.out.println("fa!equalfb");
}
if(db.equals(fb)){ //false
System.out.println("db.equalfb");
}else{
System.out.println("db!equalfb");
}
fa!=fb
fa.equalfb
db!equalfb Float 型与Double 型肯定不相等