Search

Thursday, September 3, 2015

Why you shouldn't use == with float and double in Java?

In this article, you are going to learn why you shouldn't use == with float and double in Java? Especially for checking loop termination condition. Java programmers often make mistake of using floating point number in loop and checking condition with == operator, in worst case this could create an infinite loop, causing your Java application to hung.


For example, following code will not work as you expect :

for(double balance = 10; balance!=0; balance-=0.1) {
System.out.println(balance);
}

Read more »

No comments:

Post a Comment