How to compare float and double values in Java? Example
comparing float and double values in Java is tricky; here is the right way to compare float and double in Java
Hello folks, If you have been doing Java programming then you may know that the use of == operator is not the correct way to compare floating point values in Java.
If you use equality operator to compare float and double variables then it can cause an endless loop in Java, but is there a way to prevent that loop from running infinitely? Yes, instead of using ==
operator, you can use relational operator e.g. less than (<) or greater than (>) to compare float and double values.
By changing the above code as following, you can prevent the loop from infinitely :
for(double balance = 10; balance > 0; balance-=0.1) {
System.out.println(balance);
}
If you think little bit than you will realize that greater than will definitely end this loop, but don’t expect it to print numbers like 9.9, 9.8, 9.7
because floating point numbers are just approximation, they are not exact. Some numbers e.g. 1/3 cannot be represented exactly using float and double in Java.
After running following program in your computer you may end up with something like this
By the way, if you are new to Java programming language or want to improve Java skills then you can also checkout following best Java courses to get better:
- The Complete Java Masterclass (covers Java 17)
- Java Programming and Software Engineering Fundamentals Specialization Certificate on Coursera
- Java Programming Bootcamp: Zero to Mastery
- The Complete Java Programming Masterclass! [Karpado]
- CodeGym (learn Java by building Games)
These are my favorite online courses and platforms to learn Java from scratch and also build your Java skills. If you need more advanced courses to take your Java skill to next level you can also see following articles:
Java Program to compare float and double values
Here is our complete Java program to compare any float and double values in Java:
public class FloatComparator {
public static void main(String args[]){
float firstValue = 10.2f;
float secondValue = 10.3f;
float thirdValue = 10.2f;
if(firstValue > secondValue){
System.out.print("First Value and second value are not equal");
}
}
}
By the way, this is not the only way, you can also use equals()
method of Wrapper classes like Float and Double to do the floating point comparison.
They are in fact preferred way if you want to check if two floating point values are equal or not but if you just want to know that they are not equal then using <
and >
operator is rather easy.
Java and Spring Interview Preparation Material
Before any Java and Spring Developer interview, I always read the below two resources
Grokking the Java Interview
Grokking the Java Interview: click here
I have personally bought these books to speed up my preparation.
You can get your sample copy here, check the content of it and go for it
Grokking the Java Interview [Free Sample Copy]: click here
If you want to prepare for the Spring Boot interview you follow this consolidated ebook, it also contains microservice questions from spring boot interviews.
Grokking the Spring Boot Interview
You can get your copy here — Grokking the Spring Boot Interview
That’s all about right way to compare float and double values in loop in Java. This simple trick of using logical operator less than and greater than instead of equality operator to compare float and double variable can save you a lot of headache.
If you like my tutorial, please follow me to get notified when I publish the next article. Thanks
By the way, if you are new to Java programming language or want to improve Java skills then you can also checkout following best Java courses to get better:
- The Complete Java Masterclass (covers Java 17)
- Java Programming and Software Engineering Fundamentals Specialization Certificate on Coursera
- Java Programming Bootcamp: Zero to Mastery
- The Complete Java Programming Masterclass! [Karpado]
- CodeGym (learn Java by building Games)
These are my favorite online courses and platforms to learn Java from scratch and also build your Java skills. If you need more advanced courses to take your Java skill to next level you can also see following articles: