Java Integer Class | Methods, Example

Integer class in Java is a wrapper class that wraps (converts) a value of primitive data type “int” in an object.

In simple words, an Integer is a wrapper around an int.

An object of Integer class contains a single field of type int. In this field, we can store a primitive int value.

Integer class was introduced in JDK 1.0. It is present in java.lang.Integer package.

Java Integer class declaration


The general declaration of Integer class in Java is as follows:

public final class Integer
  extends Number
    implements Comparable<Integer>

Integer class extends Number class and implements Serializable and Comparable<Integer> interfaces. Number class extends Object class.

Integer Field Constants


Java Integer class provides several field constants that are as follows:

1. static int MAX_VALUE: It represents a constant that can hold the maximum value of int (231 – 1).

2. static int MIN_VALUE: It represents a constant that can hold the minimum value of short (-231 ).

3. static int SIZE: It is the number of bits that are used to represent an int value in two’s complement binary form. It was introduced in JDK 1.5.

4. static Class<Short> TYPE: It is a Class instance that represents the primitive type short. It was introduced in JDK 1.1 version.

Java Integer Class Constructor


Integer class in java provides two constructors that are as follows:

1. Integer(int num): This form of constructor accepts int number as its parameter and converts it into an Integer class object. The general syntax to create Integer class object is as follows:

Integer obj = new Integer(123000); // Here, we are converting a primitive int value into Integer object. This is called boxing in java.

2. Integer(String str): This constructor accepts a parameter of String type and converts that string into Integer class object. The string contains an int value.

The general syntax to create an Integer object by wrapping a string that contains an int number, as follows:

String str = "198665";
Integer obj = new Integer(str);

Important Integer Class Methods in Java


In addition to methods inherited from Number class and Object class, Java Integer class also provides some useful important methods. They are as follows:

1. int compareTo(Integer obj): This method is used to compare the values of two Integer class objects numerically. It is called using the below syntax.

int x = obj1.compareTo(obj2);

where, obj1 and obj2 are Integer class objects.

  • If obj1 == obj2, this method returns 0.
  • If obj1 < obj2, it returns negative value.
  • If obj1 > obj2, it returns positive value.

2. boolean equals(Object obj): This method is used to compare Integer object with another Integer object obj. If both contain the same content, this method returns true otherwise returns false.

3. static int parseInt(String str): This method returns the primitive int value contained in the string argument str.

4. String toString(): The toString() method converts Integer object into String object and returns that String object. It returns a string form of Integer object.

5. static Integer valueOf(String str): It converts string str containing some int value into Integer class object and returns that Integer object. In other words, it returns an Integer object holding a value given by the specified String.


6. static Integer valueOf(int i): This method converts the primitive int value into Integer class object. In other words, it returns an Integer object representing the specified int value.

7. static int compare(int x, int y): It is used to compare two int values numerically.

8. static Integer decode(String str): This method decodes a String into an Integer.

9. static String toBinaryString(int i): This method is used to convert a decimal integer value i into binary number system. After converting, it returns that binary number in the form of string.

10. static String toHexString(int i): This method is used to convert a decimal integer value i into hexadecimal number system. After converting, it returns that hexadecimal number in the form of string.


11. static String toOctalString(int i): This method is used to convert a decimal integer value i into octal number system. After converting, it returns that octal number in the form of a string.

12. int intValue(): This method warps (converts) Integer object into primitive int type value. This conversion is called unboxing in java.

13. static Integer valueOf(int i): It returns an Integer object that contains the specified int value.

14. static int signum(int i): This method returns the signum function of the specified int value.

15. static int reverse(int i): This method returns the value obtained by reversing the order of the bits in the two’s complement binary representation of the specified int value.


16. static int bitCount(int i): This method is used to retrieve the number of set bits in two’s complement of the specified integer.

17. static int reverse(int i): This method returns a primitive int value reversing the order of bits in two’s complement form of the specified int value.

18. static int reverseBytes(int i): This method returns a primitive int value reversing the order of bytes in two’s complement form of the specified int value.

Example Program based on Integer Class


1. Let’s take an example program where we will accept an integer number from the keyboard and will convert it into other number systems such as binary, hexadecimal, and octal format. Look at the program source code to understand better.

Program source code 1:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class IntegerTest {
public static void main(String[] args) throws IOException
{
// Accept an Integer number from keyboard. 	
   InputStreamReader sr = new InputStreamReader(System.in);
   BufferedReader bf = new BufferedReader(sr);
 
System.out.println("Enter an integer number:");
String str = bf.readLine();

// Converting string into int type.
  int i = Integer.parseInt(str);
  System.out.println("In decimal format: " +i);
  
// Converting into other number systems.
    str = Integer.toBinaryString(i);
    System.out.println("In binary format: " +str);
    
    str = Integer.toHexString(i);
    System.out.println("In hexdecimal format: " +str);
    
    str = Integer.toOctalString(i);
    System.out.println("In octal format: " +str);
   }
}
Output:
     Enter an integer number:
     20
     In decimal format: 20
     In binary format: 10100
     In hexdecimal format: 14
     In octal format: 24

2. Let’s take another example program based on the various methods provided by Integer class. Look at the source code.

Program source code 2:

public class IntegerTest {
public static void main(String[] args)
{
 int x = 60; 
 String xx = "35"; 
   
System.out.println("toString(x) = " + Integer.toString(x)); 
 
Integer z = Integer.valueOf(x); 
System.out.println("valueOf(x) = " + z); 
   z = Integer.valueOf(xx); 
 System.out.println("valueOf(xx) = " + z); 
 
 int zz = Integer.parseInt(xx); 
 System.out.println("parseInt(xx) = " + zz); 
 
// Call decode() method to decode the binary, hexa, octal and decimal.  
// string to corresponding int values. 
   String decimal = "20";
   String binary = "0100";
   String octal = "005"; 
   String hex = "0x0f"; 

 Integer dec = Integer.decode(decimal); 
 System.out.println("decode(45) = " + dec); 
 
 dec = Integer.decode(binary);
 System.out.println("decode(0100): " +dec);
 
 dec = Integer.decode(octal); 
 System.out.println("decode(005) = " + dec); 
 
 dec = Integer.decode(hex); 
 System.out.println("decode(0x0f) = " + dec);  
  }
}
Output:
      toString(x) = 60
      valueOf(x) = 60
      valueOf(xx) = 35
      parseInt(xx) = 35
      decode(45) = 20
      decode(0100): 64
      decode(005) = 5
      decode(0x0f) = 15

Program source code 3:

public class ByteTest {
public static void main(String[] args)
{
 int x = 60; 
 String xx = "35"; 
   
// Create two Integer objects. 
  Integer i1 = new Integer(x); 
  Integer i2 = new Integer(xx); 

 System.out.println("byteValue(): " + i1.byteValue()); 
 System.out.println("shortValue(): " + i1.shortValue()); 
 
 System.out.println("intValue(): " + i1.intValue()); 
 System.out.println("longValue(): " + i1.longValue()); 
 
 System.out.println("doubleValue(): " + i1.doubleValue()); 
 System.out.println("floatValue(): " + i1.floatValue());
 
 int value = 45576; 
 
// Call bitcount() method to count set of bits in twos complement form of the number.
   System.out.println("bitcount(value)=" + Integer.bitCount(value));  
   
// Call reverse() method to reverse order of bits. 
  System.out.println("reverse(value): " +Integer.reverse(value)); 

// Call reverseBytes() method to reverse order of bytes.
   System.out.println("reverseBytes(value): " +Integer.reverseBytes(value)); 
     
// signum() returns -1,0,1 for negative,0 and positive values 
    System.out.println("signum(value): " + Integer.signum(value)); 
 
// hashcode() returns hashcode of the object 
     int hash = i1.hashCode(); 
     System.out.println("hashcode(): " + hash); 

// Call equals() method that will return a boolean value representing equality. 
    boolean eq = i1.equals(i2); 
    System.out.println("i1.equals(i2): " + eq); 

// Call compare() method used for comparing two int values.
     int comp = Integer.compare(i1, i2); 
     System.out.println("compare(i1,i2): " +comp); 

// Call compareTo() method for comparing this value with some another value. 
     int compareTo = i1.compareTo(i2); 
     System.out.println("i1.compareTo(i2):  " + compareTo);   
  }
}
Output:
       byteValue(): 60
       shortValue(): 60
       intValue(): 60
       longValue(): 60
       doubleValue(): 60.0
       floatValue(): 60.0

       bitcount(value)=5
       reverse(value): 273481728
       reverseBytes(value): 145883136

       signum(value): 1
       hashcode(): 60
       i1.equals(i2): false
       compare(i1,i2): 1
       i1.compareTo(i2):  1

Hope that this tutorial has covered important key points related Integer class in Java with example programs. I hope that you will have understood this topic.
Thanks for reading!!!
Next ⇒ Java Long Class

⇐ Prev Next ⇒