• Integer is a built-in class in Java, available in java.lang package.
• It is a Wrapper class which is dedicated to int primitive data type.
• Java.lang.Integer class entities are immutable.
class A{
public static void main(String arg[]){
Integer i1 = new Integer (10);
Integer i2 = new Integer (20);
Integer i3 = i1+i2;
System.out.println("i1 == i3 :- "+(i1 == i3)); // false
System.out.println("i2 == i3 :- "+(i2 == i3)); // false
}
}
i1 == i3 :- false i2 == i3 :- false
In the above example we can see that we have two different objects of Integer class that are i1 and i2 and in the next line we are adding i1 and i2 and assigning their result in Integer i3. Finally we are comparing i1 and i3, i2 and i3 and their corresponding results are false and false respectively. This proves that no changes have taken place in i1 and i2, which are the existing objects of java.lang.Integer class into the new object of Integer class.
public final class java.lang.Integer extends java.lang.Number implements java.lang.Comparable<java.lang.Integer> {
public static final int MIN_VALUE;
public static final int MAX_VALUE;
public static final java.lang.Class<java.lang.Integer> TYPE;
static final char[] digits;
static final char[] DigitTens;
static final char[] DigitOnes;
static final int[] sizeTable;
public static final int SIZE;
public static final int BYTES;
public static java.lang.String toString(int, int);
public static java.lang.String toUnsignedString(int, int);
public static java.lang.String toHexString(int);
public static java.lang.String toOctalString(int);
public static java.lang.String toBinaryString(int);
static int formatUnsignedInt(int, int, char[], int, int);
public static java.lang.String toString(int);
public static java.lang.String toUnsignedString(int);
static void getChars(int, int, char[]);
static int stringSize(int);
public static int parseInt(java.lang.String, int) throws java.lang.NumberFormatException;
public static int parseInt(java.lang.String) throws java.lang.NumberFormatException;
public static int parseUnsignedInt(java.lang.String, int) throws java.lang.NumberFormatException;
public static int parseUnsignedInt(java.lang.String) throws java.lang.NumberFormatException;
public static java.lang.Integer valueOf(java.lang.String, int) throws java.lang.NumberFormatException;
public static java.lang.Integer valueOf(java.lang.String) throws java.lang.NumberFormatException;
public static java.lang.Integer valueOf(int);
public java.lang.Integer(int);
public java.lang.Integer(java.lang.String) throws java.lang.NumberFormatException;
public byte byteValue();
public short shortValue();
public int intValue();
public long longValue();
public float floatValue();
public double doubleValue();
public java.lang.String toString();
public int hashCode();
public static int hashCode(int);
public boolean equals(java.lang.Object);
public static java.lang.Integer getInteger(java.lang.String);
public static java.lang.Integer getInteger(java.lang.String, int);
public static java.lang.Integer getInteger(java.lang.String, java.lang.Integer);
public static java.lang.Integer decode(java.lang.String) throws java.lang.NumberFormatException;
public int compareTo(java.lang.Integer);
public static int compare(int, int);
public static int compareUnsigned(int, int);
public static long toUnsignedLong(int);
public static int divideUnsigned(int, int);
public static int remainderUnsigned(int, int);
public static int highestOneBit(int);
public static int lowestOneBit(int);
public static int numberOfLeadingZeros(int);
public static int numberOfTrailingZeros(int);
public static int bitCount(int);
public static int rotateLeft(int, int);
public static int rotateRight(int, int);
public static int reverse(int);
public static int signum(int);
public static int reverseBytes(int);
public static int sum(int, int);
public static int max(int, int);
public static int min(int, int);
public int compareTo(java.lang.Object);
static {};
}
We can see that in the above API of java.lang.Integer class we have different utility methods or variables to perform different operations along with int type of values.
Here we will discuss about the important members of java.lang.Integer class: -
Access Modifier: - public
Member Type: - static final variable
Data Type: - int
variable Name: - MIN_VALUE
Functionality: - This member stores the minimum value (-2147483648) into the int type of variable and the same minimum value can be passed as an argument into the java.lang.Integer class constructor for creating an object.
When we try to store a value which is less than the minimum value (-2147483649), then we get error message at compile-time that shows up as: Integer number is too large. While creating an object of java.lang.Integer class, we will get this same error message if we pass into a constructor an argument of value lesser than the lower limit.
class JTC{
public static void main(String arg[]){
System.out.println("Integer.MIN_VALUE :- "+Integer.MIN_VALUE);
int i1 = -2147483648;
System.out.println("i1 :- "+i1);
//int i2 = -2147483649; --> integer number too large
Integer i3 = new Integer(-2147483648);
System.out.println("i3 :- "+i3);
// Integer i4 = new Integer(-2147483649); --> integer number too large
}
}
Integer.MIN_VALUE :- -2147483648 i1 :- -2147483648 i3 :- -2147483648
In the above example we can see that in JTC class we have declared an int type of variable, which is int i1, and have assigned it a value of -2147483648. Since -2147483648 is in the range of int primitive data type, so the statement is valid.
In the next line we have one more variable int i2 = -2147483649 and, as we discussed, -2147483649 is not in the range of short; so here we get compile time error which appears on the screen as: Integer Number too large.
Then after we are creating an object of java.lang.Integer class (Integer i3 = new Short (-2147483648)) and we can see that we are passing into the java.lang.Integer class constructor an argument of value -2147483648, which is valid as it is in the range of int values.
Finally in the last section of the above example we are creating an object of java.lang.Integer class with -2147483649 as argument, here we will get compile time error because this value is not in the range of int values.
Access Modifier: - public
Member Type: - static final variable
Data Type: - int
Variable Name: - MAX_VALUE
Functionality: - This member stores the maximum value (2147483647) in the int type of variable and this same maximum value can be passed as an argument into the java.lang.Integer class constructor while creating an object.
When we try to store a value which is greater than the maximum value (2147483647) then we get compile-time error that shows up as: Integer number is too large; and the same error occurs when we use that value to create an object of java.lang.Integer.
class JTC{
public static void main(String arg[]){
System.out.println("Integer.MAX_VALUE :- "+Integer.MAX_VALUE);
int i1 = 2147483647;
// int i2 = 2147483648; //--> Integer number is too large
Integer i3 = new Integer(2147483647);
// Integer i4 = new Integer(2147483648); //--> Integer number is too large
}
}
Integer.MAX_VALUE :- 2147483647
In the above example we can see that in JTC class we have declared an int type of variable, which is int i1, and have assigned it a value of 2147483647. The statement is valid as 2147483647 is in the range of int primitive data type values.
In the next line we have one more variable int i2 = 2147483648 and here we get error at compile time, as 2147483648 is not in the range of int values.
Then after we are creating an object of java.lang.Integer class (Integer i3 = new Integer (“2147483648”)) and we can see that we are passing 2147483648 as argument into java.lang.Integer class constructor, the statement is valid as the argument value is in the range of int .
Finally in the last section of the above example we are creating an object of java.lang.Integer class with 2147483648 as argument. Here, since the value is not in the range of int values, we will get compile time error which will show up as: Integer number as too large.
Access Modifier: - public
Member Type: - static Method
Return Type: - int
Method Name: - parseInt
Parameter: - java.lang.String
Functionality: - We have earlier known that we cannot type-cast a string type of value into int type. Nevertheless, as per our business logic, at times we may need to convert the string literals into int type. We can make this conversion by using parseInt() method. While using parseInt() we should keep in mind two important points- firstly, the argument of parseInt(String) should contain numeric value and secondly this numeric value has to be in the range of byte i.e., 2147483648 to 2147483647, otherwise we will get an exception which will appear as java.lang.NumberFormatException.
class JTC{
public static void main(String arg[]){
int i1 = Integer.parseInt("20");
System.out.println("i1 :- "+i1);
// Integer.parseInt("-2147483649"); --> java.lang.NumberFormatException
// Integer.parseInt("abc"); --> java.lang.NumberFormatException
}
}
i1 :- 20
In the above example we can see that in the first invocation of parseInt() method we are passing “20” as argument and this statement is valid because argument is in number format and 20 is in the range of short .
In the next two invocations of parseInt() method, we are passing the string values- “-2147483649” and “abc”- as arguments. As we discussed ,“-2147483649” is not in the range of int and “abc” is not in number format, so for both the invocations we get java.lang.NumberFormatException at the time of program execution.
Access Modifier: - public
Member Type: - Instance Method
Return Type: - int
Method Name: - hashCode()
Parameter: - No parameter
Functionality: - hashCode() method is overridden from java.lang.Object class in java.lang.Integer class. In java.lang.Integer class the method returns the same value which we passed as an argument into the Integer class constructor while creating the current working object of java.lang.Integer class.
class JTC{
public static void main(String arg[]){
Integer i1 = new Integer("32767");
System.out.println("i1 hashcode :- "+i1.hashCode());
}
}
i1 hashcode :- 32767
In the above example we are creating an object of java.lang.Integer class and passing “32767” as an argument (Integer i1 = new Integer (“32767”)) and in the next line we are invoking hashCode() along with i1. As we discussed earlier, hashCode() method is overridden in java.lang.Integer class and that is the reason that here we are getting 32761 as the output.
Access Modifier: - public
Member Type: - Instance Method
Return Type: - boolean
Method Name: - equals()
Parameter:- Object
Functionality: - equals() method is overridden in java.lang.Integer class, and here it compares given object to the specified object. The result is true if and only if the argument is not null and is an Integer object that contains the same int value as the specified object.
class A{
public static void main(String arg[]){
Integer i1 = new Integer(10);
Integer i2 = new Integer(20);
System.out.println("i1.equals(i2) :- "+(i1.equals(i2))); // false
Integer i3 = new Integer("10");
System.out.println("i1.equlas(i3) :- "+(i1.equals(i3))); // true
}
}
i1.equals(i2) :- false i1.equlas(i3) :- true
In this example we are creating two different objects of Integer class : Integer i1 = new Integer (10) and Integer i2 = new Integer (20); here we get false as result because we passed two different values while creating objects of java.lang.Integer class.
In the next line again, we are creating an object of Integer class (Integer i3 = new Integer (10)) and then after we are comparing i1 and i3 using equals () method; here, we get true as the result because both objects contain the same value .
Access Modifier: - public
Member Type: - Instance Method
Return Type: - int
Method Name: - compareTo()
Parameter:- java.lang.Short
Functionality: - The method returns 0 if given integer is equal to the argument integer; a value less than 0 if given integer is numerically less than the argument integer; and a value greater than 0 if given integer is numerically greater than the argument integer.
class JTC{
public static void main(String arg[]){
Integer i1 = new Integer ("10");
Integer i2 = new Integer ("10");
System.out.println("i1.compareTo(i2) :- "+(i1.compareTo(i2))); // 0
Integer i3 = new Integer ("11");
System.out.println("i1.compareTo(i3) :- "+(i1.compareTo(i3))); // -1
Integer i4 = new Integer ("9");
System.out.println("i1.compareTo(i4) :- "+(i1.compareTo(i4))); // 1
}
}
i1.compareTo(i2) :- 0 i1.compareTo(i3) :- -1 i1.compareTo(i4) :- 1
In the above example we are creating two different objects of java.lang.Integer with same value which is 10 and that is the reason when we compare i1 and i2 we get 0.
In the next line we are creating an object of java.lang.Integer with 11 (Integer i3 = new Integer (11)) and as we can see that i1 has int value 10 and that is the reason when we compare i1 and i3, we get -1.
Finally, in the last line we are creating an object of Integer which is Integer i4 = new Integer (9), that is the reason we are getting 1 as a result of i1.compareTo(i4).
Access Modifier: - public
Member Type: - Static Method
Return Type: - String
Method Name: - toHexString()
Parameter:- int
Functionality: - The method converts the argument int into a hexadecimal and then returns the value as a String.
class JTC{
public static void main(String arg[]){
String s1 = Integer.toHexString(10);
System.out.println("s1 :- "+s1);// a
}
}
s1 :- a
In this example we can see that we are invoking toHexString() method and passing 10 as an argument and, as we discussed, the method converts 10 into hexadecimal and returns the value as String and thereby we get a (10 in decimal).
Access Modifier: - public
Member Type: - Static Method
Return Type: - String
Method Name: - toBinaryString()
Parameter:- int
Functionality: - The method converts the argument int into binary and then returns the value as a String.
class JTC{
public static void main(String arg[]){
String s1 = Integer.toBinaryString(10);
System.out.println("s1 :- "+s1);// 1010
}
}
s1 :- 1010
In this example we can see that we are invoking toBinaryString() method and passing 10 as an argument, and as we discussed, the method converts 10 into binary and returns the value as a String and thereby we get 1010 (10 in decimal).
Access Modifier: - public
Member Type: - Static Method
Return Type: - String
Method Name: - toOctalString()
Parameter:- int
Functionality: - The method converts the argument int into octal and then returns the value as a String.
class JTC{
public static void main(String arg[]){
String s1 = Integer.toOctalString(10);
System.out.println("s1 :- "+s1);// 12
}
}
s1 :- 12
In this example we can see that we are invoking toOctalString() method and passing 10 as an argument and as we discussed the method converts 10 into octal and thereafter returns the value as String and thereby we are get 12 (10 in decimal).