How to compare Java Number instances – J048

by Jun 7, 2016

Transcript – How to compare Java Number instances

We need to know how to compare Java Number instances. It’s different than how you compare number primitives in Java.

In this lesson we’re going to look at comparing Java Number instances. For starters, since Numbers are objects, we can’t use the regular arithmetic operators. When we use the primitive operators with objects, we’re comparing the instances – not the values inside the instance.

Integer a = 42;
Integer b = 42;

if (a == b) {
    System.out.println(“a and b are equal”);
} else {
    System.out.println(“a and b are NOT equal”);

Be careful, because sometimes you’ll get tricked into thinking you’re comparing instances. For example if we create two Integers, both with the value of 10, an equality comparison will equal true. Java’s doing some sneaky stuff behind the scenes, and this fails for larger integers. Java just caches the Integer instances from -127 to 128. Lesson learned, the arithmetic operators are comparing instances.

When we want to see if two Integer values are equal, the best option is use the equals() method. This is the same equals() method we saw in earlier class tutorials.

If we’re comparing the values of two Integers, we want to get the value out like this using the intValue() method. This returns the value as a primitive. Then we can use any of the regular arithmetic operators.

Java has a standard way for sorting objects. In order to sort object values, Java needs one method to compare objects.

The first compare method we’ll look at is compareTo(). This method is part of the interface Comparable. A comparable object is capable of comparing itself with another object. The reason to implement the Comparable interface is for sorting lists of objects, in our case lists of the Integer class. The Comparable interface expects a method with the same signature as compareTo(). The parameter is always the same object type.

In the Integer class, compareTo() expects another Integer. The method returns 0 if they are equal, -1 if the Integer is smaller than the Integer provided as a parameter, and 1 if the Integer is greater. This is used in the sort method for most collections. We call sort, and the collection will sort the group of objects using the compareTo() method.

Integer[] array = {10, 9, 8, 4, 6, 5, 1, 2}; // these are objects!!
System.out.println("Initial " + Arrays.toString(array));
System.out.println("Final   " + Arrays.toString(array));

Here’s how we’d sort an array of Integers. Define our array. Then use the Arrays class and call the sort method. Our array is now sorted. The sort method just takes an array of Objects, so how does it know how to sort our Integers? It’s using the compareTo() method.

Finally there is a method to compare two integers as if they were unsigned. When Java was created, for better or worse, they didn’t include unsigned numbers. They thought unsigned numbers were too complicated and not really needed. In Java 8, they changed their minds. It was too late to add a new primitive type, so they kinda smashed a weird solution into the Integer and Long classes using static methods. So we have a compareUnsigned method.

This method compares two Integers as if they were unsigned. That means the sign bit is also used for the magnitude. The catch is we’re still using Integer instances. The numbers are still signed values, they just added methods that pretend the value is unsigned. We’ll need more methods to access the bits of the Integer values. We’ll look at unsigned numbers and bit manipulation using the Integer class in the next tutorial.

If you have any questions about comparing Java number class instances, add them to the comments below. Next we’ll look at bit manipulation in the Java Number classes. If you liked this video, please like it and share. That helps get the video in front of more views. You’ll also want to make sure you sign up for the DeegeU newsletter. I’ll send the newsletter out once a month, and it will contain the month’s videos, plus tips and news not contained in the videos!

And with that, I’ll see you in the next tutorial!

<p><script async="" src="//"></script>
<!-- DeegeU - Right Side -->
<ins class="adsbygoogle" style="display:inline-block;width:336px;height:280px" data-ad-client="ca-pub-5305511207032009" data-ad-slot="5596823779"></ins>
(adsbygoogle = window.adsbygoogle || []).push({});

Tools Used

  • Java
  • NetBeans

Media Credits

All media created and owned by DJ Spiess unless listed below.

  • No infringement intended

Rhastafarian by Audionautix is licensed under a Creative Commons Attribution license (

Get the code

The source code for “Are you ready to tackle the fizzbuzz test in Java?” can be found on Github. If you have Git installed on your system, you can clone the repository by issuing the following command:

 git clone

Go to the Support > Getting the Code page for more help.

If you find any errors in the code, feel free to let me know or issue a pull request in Git.

<h2>Don't miss another video!</h2> <p>New videos come out every week. Make sure you subscribe!<br><script src="//"></script></p> <div class="g-ytsubscribe" data-channel="deegeu" data-layout="full" data-count="default"></div> <p></p>



DJ Spiess

DJ Spiess

Your personal instructor

My name is DJ Spiess and I'm a developer with a Masters degree in Computer Science working in Colorado, USA. I primarily work with Java server applications. I started programming as a kid in the 1980s, and I've programmed professionally since 1996. My main focus are REST APIs, large-scale data, and mobile development. The last six years I've worked on large National Science Foundation projects. You can read more about my development experience on my LinkedIn account.

Pin It on Pinterest

Share This