if/else statement
Compare integers
With this example we are going to demonstrate how to compare integers. The comparation between two integers can be performed using thegreater than andless than symbols. In short, to compare two integersi1 andi2 you should:
- Check if
i1is greater thani2in anifstatement. If it is true, that means thati1is greater thani2. - Check if
i1is less thani2in anelse ifstatement. If it is true, that means thati1is less thani2. - Code inside the
elsestatement will be executed when aboveifstatements are both false.
Let’s take a look at the code snippet that follows:
package com.javacodegeeks.snippets.basics;public class CompareIntegers {public static void main(String[] args) {int i1 = 12;int i2 = 43;if (i1 > i2) {System.out.println(i1 + " is greater than " + i2);}else if (i1< i2) {System.out.println(i1 + " is less than " + i2);}else {System.out.println(i1 + " is equal to " + i2);}}}Output:
12 is less than 43
This was an example of how to compare integers in Java.
Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to theTerms andPrivacy Policy

Thank you!
We will contact you soon.
Ilias Tsagklis
Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor atJava Code Geeks.



