java.lang.Integer

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:

Integer secondInteger = new Integer(100);

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 sampleInteger = 100;

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

		Integer firstInteger = 100;
		Integer secondInteger = new Integer(100);
		System.out.println(firstInteger==secondInteger);

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

package com.javatutorialhq.java.examples;

/*
 * Example program to test equality of Integer object
 */

public class IntegerTestEquality {

	public static void main(String[] args) {

		// instantiate Integer objects
		Integer firstInteger = 100;
		Integer secondInteger = new Integer(100);
		
		// test for equality
		System.out.println(firstInteger.equals(secondInteger));		

	}

}

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

Integer.MAX_VALUE

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

Integer.MIN_VALUE

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

Integer intValue = Integer.parseInt("2147483648");

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

Exception in thread "main" java.lang.NumberFormatException: For input string: "2147483648"
	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
	at java.lang.Integer.parseInt(Integer.java:587)
	at java.lang.Integer.parseInt(Integer.java:615)
	at com.javatutorialhq.java.examples.IntegerTestEquality.main(IntegerTestEquality.java:12)

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:

		Integer intValue = Integer.parseInt("2147");
		System.out.println(intValue.byteValue());

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

System.out.println(Integer.max(1243, 1245));

Did you notice the difference on how the methods are being called? Non static methods are called variable.method() while static methods are called Integer.method(arguments….). Don’t take this for granted otherwise our java compiler will give a compilation error.

Now that we have discussed the basics of Integer class we now go through in details on different fields and methods available.

Available Field of an Integer Object

Modifier Type Field Description
static int MAX_VALUE A constant which is the maximum value of int data type and can be equal to 2^31-1.
static int MIN_VALUE A constant which is the minimum value of int data type and can be equal to -2^31.
static int SIZE The number of bits used to represent an int value in two\'s complement binary form.
static Class<Integer> TYPE The Class instance representing the primitive type int.

Integer Class Constructor

Syntax Description
Integer(int value) This constructs a newly allocated Integer object that represents the specified int value.
Integer(String s) This constructs a newly allocated Integer object that represents the int value as specified by the String constructor parameter.

Integer Examples by Method Usage

Modifier Return Type Method Description
static int bitCount(int i) This method return an int which correspond to the count of the one’s bits of the 2′s complement of the int method argument.
byte byteValue() returns the value of this Integer object as byte. This method override the byteValue() method of Number class.
static int compare(int x, int y) This method simply returns the int equivalent of the result of comparing the two int method argument.
int compareTo(Integer anotherInteger) return an int which corresponds to the equality of the method argument and this Integer.
static Integer decode(String nm) returns a the decoded value into Integer object of the String method argument.
double doubleValue() This method returns the double equivalent of this Integer object. The method doubleValue() were inherited from Number class.
boolean equals(Object obj) return boolean which corresponds to the equality of this Integer and method argument Object. This method override the equals method of Object class.
float floatValue() This method returns the float equivalent of this Integer object. The method floatValue were inherited from Number class.
static Integer getInteger(String nm) The getInteger(String nm) method simply returns the value of the property key specified as method argument interpreted as an Integer object.
static Integer getInteger(String nm, int val) Determines the integer value of the system property with the specified name.
static Integer getInteger(String nm, Integer val) This method returns the Integer value of System.property that has been specified as method argument or if the property has not been found then the val specified will be returned instead.
int hashCode() Returns a hash code for this Integer.
static int highestOneBit(int i) The highestOneBit(int i) method simply returns the int value with a single one-bit, in the position of the highest-order one-bit in the specified value, or zero if the specified value is itself equal to zer0
int intValue() This method returns the int equivalent of this Integer object. The method intValue were inherited from Number class.
long longValue() Returns the value of this Integer as a long.
static int lowestOneBit(int i) Returns an int value with at most a single one-bit, in the position of the lowest-order ("rightmost") one-bit in the specified int value.
static int numberOfLeadingZeros(int i) This method returns an int which corresponds to the number of zeroes on the left most part of the 2′s complement equivalent of int method argument.
static int numberOfTrailingZeros(int i) This method returns an int which corresponds to the number of zeroes on the right most part of the 2′s complement equivalent of int method argument.
static int parseInt(String s) Parses the string argument as a signed decimal integer.
static int parseInt(String s, int radix) Parses the string argument as a signed integer in the radix specified by the second argument.
static int reverse(int i) Returns the value obtained by reversing the order of the bits in the two\'s complement binary representation of the specified int value.
static int reverseBytes(int i) Returns the value obtained by reversing the order of the bytes in the two\'s complement representation of the specified int value.
static int rotateLeft(int i, int distance) returns the value obtained by rotating the two’s complement binary representation of the specified int value left by the specified number of bits. (Bits shifted out of the left hand, or high-order, side reenter on the right, or low-order.)
static int rotateRight(int i, int distance) This method returns Returns the value obtained by rotating the two’s complement binary representation of the specified int value right by the specified number of bits. (Bits shifted out of the right hand, or low-order, side reenter on the left, or high-order.)
short shortValue() returns the short equivalent of the Integer object. This method override the shortValue() method of Number class.
static int signum(int i) Returns the signum function of the specified int value.
static String toBinaryString(int i) The toBinaryString(int i) method simply returns the binary string equivalent of int method parameter.
static String toHexString(int i) Returns a string representation of the integer argument as an unsigned integer in base 16.
static String toOctalString(int i) Returns a string representation of the integer argument as an unsigned integer in base 8.
String toString() Returns a String object representing this Integer\'s value.
static String toString(int i) Returns a String object representing the specified integer.
static String toString(int i, int radix) returns a string equivalent of the the int method argument with consideration the on the radix specified.
static Integer valueOf(int i) Returns an Integer instance representing the specified int value.
static Integer valueOf(String s) Returns an Integer object holding the value of the specified String.
static Integer valueOf(String s, int radix) Returns an Integer object holding the value extracted from the specified String when parsed with the radix given by the second argument.