Integer Wrapper class in Java

We have wrapper classes in Java that provide a mechanism to wrap primitive values in an object so that primitives can be used for the operations reserved only for objects. One such wrapper class is the Integer wrapper class which wraps an int primitive to facilitate object operations.

The Integer class is present in java.lang package.

Declaration of the Integer class

public final class Integer extends Number
        implements Comparable<Integer>, Constable, ConstantDesc

Here, Number is the abstract class that has declared xxxValue() methods like intValue(), floatValue(), etc. which is then implemented by the Integer class.

How to make an object of the Integer class?

We can make the objects of the Integer class in several ways

Let’s look at all of the above methods, and we will also see which of the above methods should be preferred to make objects of the Integer Wrapper class.

– Using Integer(int value) constructor
  • Constructor declaration – public Integer(int value)
  • What does it do? It will construct an object of the Integer wrapper class representing the value of the int primitive type passed in the arguments. This constructor has been deprecated since java 9 and is thus not recommended to use it.
Code Example
public class Codekru {

	public static void main(String[] args) {

		Integer i = new Integer(5); // creating an Integer instance
		System.out.println("Integer object value: " + i);
	}
}

Output –

Integer object value: 5

– Using Integer(String s) constructor

  • Constructor declaration – public Integer(String s) throws NumberFormatException
  • What does it do? It will construct an object of the Integer class representing the value of the string parameter passed in the arguments. This constructor is also deprecated since java 9 and is not recommended to use anymore.

The string passed must contain only the decimal characters and an optional positive or negative sign at the start. If we pass any other characters, then it will throw a NumberFormatException.


// Below are the correct ways of passing a string into the constructor's argument
 
 Integer i1 = new Integer("5"); 
 Integer i2 = new Integer("+5");
 Integer i3 = new Integer("-5");

// And below are the wrong ways

Integer i4 = new Integer("five");
Integer i5 = new Integer("5five");
Code Example
public class Codekru {

	public static void main(String[] args) {

		Integer i1 = new Integer("5");
		Integer i2 = new Integer("+5");
		Integer i3 = new Integer("-5");

		System.out.println("Integer object value for i1: " + i1);
		System.out.println("Integer object value for i2: " + i2);
		System.out.println("Integer object value for i3: " + i3);
	}
}

Output –

Integer object value for i1: 5
Integer object value for i2: 5
Integer object value for i3: -5
Internal implementation of the Integer(String s) constructor

Integer(String s) constructor internally uses the parseInt(s, 10) method.

    public Integer(String s) throws NumberFormatException {
        this.value = parseInt(s, 10);
    }

– Using valueOf() method

valueOf() method has three overloaded methods discussed in this post. The valueOf() method is the preferred way to make the Integer instance as it yields significantly better space and time performance by caching frequently requested values.

Code Example
public class Codekru {

	public static void main(String[] args) {

		Integer i1 = Integer.valueOf(12);
		Integer i2 = Integer.valueOf("15");

		System.out.println("Integer object value for i1: " + i1);
		System.out.println("Integer object value for i2: " + i2);
	}
}

Output –

Integer object value for i1: 12
Integer object value for i2: 15

Comparing and caching in Integer Class Objects

How we can compare and check the equality between two Integer class objects?

We can compare two integer objects using the equals() method or the comparison operator ( == ).

equals() method compares the primitive values stored by a wrapper instance, whereas == compares the object references only. Let’s show you this with an example.

public class Codekru {

	public static void main(String[] args) {
		Integer i1 = Integer.valueOf(1234);
		Integer i2 = Integer.valueOf(1234);

		if (i1.equals(i2)) {
			System.out.println("Both are equal");
		} else {
			System.out.println("Both aren't equal");
		}

	}
}

Output –

Both are equal
public class Codekru {

	public static void main(String[] args) {
		Integer i1 = Integer.valueOf(1234);
		Integer i2 = Integer.valueOf(1234);

		if (i1 == i2) {
			System.out.println("Both are equal");
		} else {
			System.out.println("Both aren't equal");
		}

	}
}

Output –

Both aren't equal
  • == operator only compares the references and not the values contained within the object, and that’s why i1==i2 returned false because they were two different instances. However, the value within them was the same.
  • On the other hand, the equals() method compares the value and not the references. Because of this, i1.equals(i2) returned true as both the integers have the same value despite being two different instances.
Caching in the Integer wrapper classes

Integer wrapper classes cache the wrapper objects for values between -128 to 127. So, if we again make an integer object of the values between -128 and 127, no new object will be created, and the object will be picked from the cache made by the integer class.

Let’s show you an example –

public class Codekru {

	public static void main(String[] args) {
		Integer i1 = Integer.valueOf(30);
		Integer i2 = Integer.valueOf(30);

		if (i1 == i2) {
			System.out.println("Both are equal");
		} else {
			System.out.println("Both aren't equal");
		}

	}
}

Output –

Both are equal
public class Codekru {

	public static void main(String[] args) {
		Integer i1 = Integer.valueOf(130);
		Integer i2 = Integer.valueOf(130);

		if (i1 == i2) {
			System.out.println("Both are equal");
		} else {
			System.out.println("Both aren't equal");
		}

	}
}

Output –

Both aren't equal
  • In the case of our 1st example, i2 was picked from the cache, and thus i1==i2 resulted in true value.
  • Whereas in the 2nd example, a new object was made for i2, and so, i1==i2 resulted in a false value.

Methods of the Integer wrapper class

Method Name What does it do?
public static int bitCount(int i) It will return the count of one-bits in the two’s complement binary representation of the specified int value.
public static int compare(int x, int y) It compares two int values numerically.
public static int compareUnsigned(int x, int y) It compares two int values while numerically treating the values as unsigned.
public static Integer decode(String nm) It decodes a String into an Integer.
public static int divideUnsigned(int dividend, int divisor) It returns the unsigned quotient of dividing the first argument by the second where each argument and the result is interpreted as an unsigned value.
public static Integer getInteger(String nm) It determines the integer value of the system property with the specified name.
public static int highestOneBit(int i) It returns an int value with at most a single one-bit, in the position of the highest-order (“leftmost”) one-bit in the specified int value.
public static int lowestOneBit(int i) It 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.
public static int numberOfLeadingZeros(int i) It returns the number of zero bits preceding the highest-order(“leftmost”) one-bit in the two’s complement binary representation of the specified int value, or 32 if the value is equal to zero.
public static int numberOfTrailingZeros(int i) It returns the number of zero bits preceding the lowest-order (“rightmost”) one-bit in the two’s complement binary representation of the specified int value, or 32 if the value is equal to zero.
public static int parseInt(String s) It returns the integer value represented by the argument in decimal.
public static int parseUnsignedInt(String s) It returns the unsigned integer value represented by the argument in decimal.
public static int remainderUnsigned(int dividend, int divisor) It returns the unsigned remainder of the first argument divided by the second.
public static int reverse(int i) It returns the value obtained by reversing the order of the bits in the specified int value.
public static int reverseBytes(int i) It returns the value obtained by reversing the bytes in the specified int value.
public static int rotateLeft(int i, int distance) It returns the value obtained by rotating the two’s complement binary representation of the specified int value left by the specified number of bits.
public static int rotateRight(int i, int distance) It returns the value obtained by rotating the two’s complement binary representation of the specified int value right by the specified number of bits.
public static String toBinaryString(int i) It returns the string representation of the unsigned integer value represented by the argument in binary ( base 2 ).
public static String toHexString(int i) It returns the string representation of the unsigned integer value represented by the argument in hexadecimal ( base 16 ).
public static String toOctalString(int i) It returns the string representation of the unsigned integer value represented by the argument in octal ( base 8 ).
public static long toUnsignedLong(int x) It returns the argument converted to long by an unsigned conversion.
public static String toUnsignedString(int i) It returns an unsigned string representation of the argument.
public static String toString() It returns a string representation of the argument.
public static Integer valueOf(int i) It returns an Integer instance representing i.

We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.

Related Articles
Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *