if/else statement

Compare integers

Photo of Ilias TsagklisIlias TsagklisNovember 11th, 2012Last Updated: August 20th, 2013
0 83 1 minute read

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 ifi1 is greater thani2 in anif statement. If it is true, that means thati1 is greater thani2.
  • Check ifi1 is less thani2 in anelse if statement. If it is true, that means thati1 is less thani2.
  • Code inside theelse statement will be executed when aboveif statements 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.

Photo of Ilias TsagklisIlias TsagklisNovember 11th, 2012Last Updated: August 20th, 2013
0 83 1 minute read
Photo of Ilias Tsagklis

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor atJava Code Geeks.

Related Articles

Bipartite Graph

Simple if else Java Example

November 11th, 2012
Bipartite Graph

Simple if statement

November 11th, 2012
Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.