Home > Java > javaTutorial > Common trap questions and answers in Java

Common trap questions and answers in Java

高洛峰
Release: 2017-01-16 16:20:18
Original
1682 people have browsed it

1. Find odd numbers:

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

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);
Copy after login

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);
Copy after login

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)
{
}
Copy after login

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());
Copy after login

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);
 }
Copy after login

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.
The reason why it becomes a negative number is that int overflows. Here, changing <=END to

4. What exactly is returned?

public static boolean decision() {
 try {
 return true;
 } finally {
 return false;
 }
}
Copy after login

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:

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

The output result is:

A .Compilation error B10.9 C.9 D None of the above answers are correct.

The execution result is:

value is9.0
Copy after login

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

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");
  }
Copy after login


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

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");
  }
Copy after login

The result is:

fa!=fb
fa.equalfb
db!equalfb Float 型与Double 型肯定不相等
Copy after login

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_

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

4. 466459262.iteye.com/blog/ 739300

Summary

The above is the entire content of this article. I hope the content of this article can bring some help to everyone's study or work. If you have any questions, you can leave a message to communicate.

For more articles related to common trap questions and answers in Java, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template