The Integer class wraps the int primitive data type into an object. This class includes helpful methods in converting value from string to Integer. This document is prepared to show the the full details on how to use Integer class together its methods and attributes.

Integer Class Syntax

public final class Integer
extends Number
implements Comparable<Integer>

Integer Compatibility Version

Requires JDK 1.0

Integer Class Basics

Since the Integer class has many features and functionality we have decided to divide this document into sections. I would suggest going through all of this in order to understand the overly used Integer wrapper class.

How to Instantiate Integer Wrapper Class

There are two ways to instantiate the Integer object. One is two use the new keyword. Below is a sample way on how to do this:

And the second method to create an Integer object is to use the autoboxing feature of java which directly converts a primitive data type to its corresponding wrapper class.  If you would like to go more into details, I am recommending for you to read the official java documentation regarding autoboxing and unboxing. Below is an example on how to make use of autoboxing in order to create a new Integer object.

Integer Object Test for Equality

As we have already discussed the Integer objects wraps the primitive type int. Because this is already an object type the use of = will no longer work. Lets see below code snippet

What do you think would be the result? Would it be true or false. Since we are already dealing with Integer object the == is no longer applicable. So how to test the equality between two integer object? Lets take a look below for code snippet on how to test equality

Maximum and Minimum Value of Integer

To find the maximum and minimum value of Integer, we would be invoking two if it’s fields. Maximum value of an Integer object is 2147483647 which can be found using

and the minimum value of Integer is -2147483648 which can be found also using one of the fields of Integer

So why do we care about these values? First to know the limit on what value should we assign to. Secondly if we go through on the methods of the Integer class we would find some parsing and conversion from another object type to Integer type. Lets try to take a look on the parseint(String s) method which take an argument string and convert it into Integer object. So what if we pass a string value which has an integer equivalent that is higher than the maximum value as defined by the Integer.MAX_VALUE? To have a birds eye view of this scenario lets take a look on below example

As you would have noticed we have passed a method argument to parseint method that is +1 of the maximum value which certainly breached the threshold. So if we try to run the above code snippet, our compiler will squeal a big NumberFormatException

How to Deal with Integer Static Methods

Most of the methods and Integer class are static thus it’s worth a while to understand on how to access these methods. Normally if we have a non static methods, we will be accessing it as like the following:

Static methods should not be called like the above example it should be like this: