How to Compare Integer Values in Java

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: Converting Integer to Int in Java

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
 Replay
Your next lesson will play in 10 seconds
  • 0:00 Comparing Integers
  • 1:31 If/Then/Else Statements
  • 2:10 Comparing the Methods
  • 2:54 Lesson Summary
Save Save Save

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Log in or Sign up

Timeline
Autoplay
Autoplay
Speed Speed

Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

In this lesson, we will discuss methods for comparing integers in Java. Some working code examples will be provided to help explain how a comparison of integers can be accomplished.

Comparing Integers

When comparing two integer values, Java provides a couple options. In Java, all primitive data types (such as int, float, double, and byte) have individual wrapper classes. Integer is a wrapper class of int, and it provides several methods and variables you can use in your code to work with integer variables. One of the methods is the compareTo() method. It is used to compare two integer values. It will return a -1, 0, or 1, depending on the result of the comparison. The compareTo() method will return the following values:

  • -1: Object 1 is less than Object 2
  • 0: Object 1 equals Object 2
  • 1: Object 1 is greater than Object 2

In order to use compareTo(), you need to create a new instance of an Integer class. In our first example, we will give the new class a specific value, but you could also pass in another integer variable. Therefore, you are creating an instance of an Integer. Instead of an integer (lower-case) value of 10, a whole new Integer object is created, with a value of 10, like this:


Java integer compareTo


The variables value1 and value2 are instance variables because they represent instances of the Integer class. This lets us perform the compareTo() method upon them. Since 10 is less than 20, we would expect the return value to be -1. When we run the code, that proves to be the case, as we see here:


Java comperTo output


If/Then/Else Statements

Because Java is an object-oriented language, we tend to gravitate towards methods that support objects and classes. Therefore, we use the Integer class and its methods to compare integers. But there is still a tried-and-true method for comparing integers: the good old if/then/else statements.

In the next code sample, we'll declare a couple of integers and check if they are equal, less than, or greater than each other. While we miss out on the standard function of compareTo(), we can define our own method for handling the comparison.

Recall that Java comparison operators look like this:

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support