Integer is a wrapper class for primitive int data type. This class provides several useful methods, which can be used to perform various operations on integers. In this guide, we will discuss all the methods of Java Integer class with examples.
Constructors of Integer class in Java
Constructor | Description |
public Integer(int i) | It creates an object of Integer class that contains the specified int value i . |
public Integer(String s) | It creates an object of Integer class that contains the int value represented by string s . When an object is created using this constructor, the specified string is converted into an int value. |
Java Integer class Methods
1. bitCount()
The bitCount() method returns the count of 1’s bits in the two’s complement representation of the given number.
Syntax:
public static int bitCount(int i)
Parameters:
i
– an int value whose 1’s bits are to be counted
2. byteValue()
The byteValue() method returns the given integer value in bytes after narrowing primitive conversion. This method is used when you want to convert the integer to bytes.
Syntax:
public byte byteValue()
3. compare()
The compare() method compares two primitive int values passed as an arguments to this method. It returns the value 0 if first number is equal to the second number, a value less than 0 if first number < second number; and a value greater than 0 if first number > second number.
Syntax:
public static int compare(int x, int y)
Parameters:
x
– First int number to compare.y
– Second int number to compare.
4. compareTo()
The compareTo() method compares this Integer with the Integer argument. It returns, 0 if this Integer is equal to given Integer, a value less than 0 if this Integer is less than Integer argument, a value greater than zero if this Integer is greater than Integer argument. This is a signed comparison.
Syntax:
public int compareTo(Integer anotherInteger)
Parameters:
anotherInteger
– The Integer which needs to be compared to this Integer.
5. compareUnsigned()
The compareUnsigned() method compares two primitive int values without considering their sign. It is same as compare() method except that it doesn’t take sign into consideration while comparison.
Syntax:
public static int compareUnsigned(int x, int y)
Parameters:
x
– First int number to comparey
– Second int number to compare
6. decode()
The decode() method decodes a given string into Integer object.
Syntax:
public static Integer decode(String s)
Parameters:
s
– The string argument, it represents the string which needs to be decoded into an Integer.
7. divideUnsigned
The divideUnsigned() method returns the quotient after dividing first argument by second argument. The signs of the arguments are not considered while doing division.
Syntax:
public static int divideUnsigned(int dividend, int divisor)
Parameters:
dividend
– The first int argument. The value to be divided.divisor
– The second int argument. The value that is dividing.
8. doubleValue()
The doubleValue() method returns the value of this Integer as double after performing widening primitive conversion.
Syntax:
public double doubleValue()
9. equals()
The equals() method compares this Integer object to the given object obj
. It returns true if both the objects contain same int value else it returns false.
Syntax:
public boolean equals(Object obj)
Parameters:
obj
– The given object to compare with this Integer.
10. floatValue()
The floatValue() method returns the value of this Integer object as float after performing widening primitive conversion (Integer -> float)
Syntax:
public float floatValue()
11. getInteger()
The getInteger() method returns the integer value of the system property with the specified property name.
Syntax:
public static Integer getInteger(String nm) public static Integer getInteger(String nm, int val) public static Integer getInteger(String nm, Integer val)
Parameters:
nm
– Property name.val
– Default value. This value is returned if the specified property does not exist.
12. hashCode()
The hashCode() method returns a hash code for this Integer.
Syntax:
public int hashCode() public int hashCode(int val)
Parameters:
val
– An int value whose hash code is to be determined.
13. highestOneBit()
The highestOneBit() method returns the int value with the highest one bit. This is determined by placing 1’s bit at the highest position (left most) in the given int number. For example: If the binary representation of a given number is 0000 0000 0000 0011, then this method returns the int number equivalent to 0000 0000 0000 0010.
If the given number is zero, then it returns zero.
Syntax:
public static int highestOneBit(int i)
Parameters:
i
– An int value whose highest one bit is to be determined.
14. intValue()
The intValue() method returns this Integer value as a primitive int value.
Syntax:
public int intValue()
15. longValue()
The longValue() method returns this Integer value as a long value after widening primitive conversion.
Syntax:
public long longValue()
16. lowestOneBit()
The lowestOneBit() method returns the int value with the lowest one bit. This is determined by placing 1’s bit at the lowest position (right most) in the given int number. For example: If the binary representation of a given number is 0000 0000 0000 0011, then this method returns the int number equivalent to 0000 0000 0000 0001.
If the given number is zero, then it returns zero.
public static int lowestOneBit(int i)
Parameters:
i
– An int value whose lowest one bit is to be determined.
17. max()
The max() method returns greater of two int numbers passed as arguments to this method. It works similar to Math.max() method.
Syntax:
public static int max(int a, int b)
Parameters:
a
– An int number passed as first argument to max method.b
– An int number passed as second argument to max method.
18. min()
The min() method returns smaller number between two given int numbers. It works similar to Math.min() method.
Syntax:
public static int min(int a, int b)
Parameters:
a
– First operand.b
– Second operand.
19. numberOfLeadingZeros()
The numberOfLeadingZeros() method returns number of 0’s bits before the first 1’s bit in the binary representation of the given int number.
If number is zero (there are no one-bit), then it returns 32.
public static int numberOfLeadingZeros(int i)
Parameters:
i
– An int value whose number of leading zeroes needs to be determined.
20. numberOfTrailingZeros()
The numberOfTrailingZeros() method returns number of 0’s bits after the first 1’s bit in the binary representation of the given int number.
Returns 32 if there are no one-bit in the binary representation of the given int number (which means number is zero).
public static int numberOfTrailingZeros(int i)
Parameters:
i
– An int value whose number of trailing zeroes to be computed.
21. parseInt()
The parseInt() method returns int value after parsing the specified string using the specified radix. If radix is not provided, then it uses 10 as radix.
Syntax:
public static int parseInt(String s) throws NumberFormatException public static int parseInt(String s, int radix) throws NumberFormatException
Parameters:
s
– String to be parsed.radix
– radix that is used for parsing.
22. parseUnsignedInt()
The parseUnsignedInt() method parses the given string as an unsigned integer using the specified radix. If radix is not provided, then it uses 10 as radix.
Syntax:
public static int parseUnsignedInt(String s) throws NumberFormatException public static int parseUnsignedInt(String s, int radix) throws NumberFormatException
Parameters:
s
– String representation of an unsigned int.radix
– radix that is used for parsing.
23. remainderUnsigned()
The remainderUnsigned() method returns remainder after dividing the first argument by second argument. The result is an unsigned int value.
Syntax:
public static int remainderUnsigned(int dividend, int divisor)
Parameters:
dividend
– An int value passed as first argument.divisor
– An int value passed as second argument.
24. reverse()
The reverse() method returns an int value, which is obtained after reversing the bits in the two’s complement binary representation of a given int value.
Do not confuse it with the reverse of an int number, it does not reverse the digits in the given number, this reverse is applied on the binary representation.
Syntax:
public static int reverse(int i)
Parameters:
i
– An int value whose reverse is to be determined.
25. reverseBytes()
The reverseBytes() method returns an int value, after reversing the order of bytes in the two’s complement representation of a given int value.
Syntax:
public static int reverseBytes(int i)
Parameters:
i
– An int value whose bytes are to be reversed.
26. rotateLeft()
The rotateLeft() method returns an int value, after rotating the bits of two’s complement representation of given int number to left by specified distance. During rotation, the most significant bits (the far left side bits) are moved to the least significant bits (far right side) position.
Syntax:
public static int rotateLeft(int i, int distance)
Parameters:
i
– An int value whose bits are to be rotated left.distance
– Positions to shift. For example: distance 2 means all bits will shift to left by two positions.
27. rotateRight()
The rotateRight() method returns an int value, after rotating the bits of two’s complement representation of given int number to right by specified distance. During rotation, the least significant bits (the far right side bits) are moved to the most significant bits (far left side) position.
Syntax:
public static int rotateRight(int i, int distance)
Parameters:
i
– An int value whose bits are to be rotated right.distance
– Positions to shift. For example: distance 2 means all bits will shift to right by two positions.
28. shortValue()
The shortValue() method returns the value of this Integer as short. It uses narrowing primitive conversion (Integer -> byte).
Syntax:
public short shortValue()
29. signum()
The signum() method returns signum function of the given int value. It returns 0, if the given int value is zero, -1 if the given int value is negative and 1 if the given int value is positive.
Syntax:
public static int signum(int i)
Parameters:
i
– An int value whose signum is to be determined.
30. sum()
The sum() method returns the sum of two given int values.
Syntax:
public static int sum(int a, int b)
Parameters:
- a – First operand for + operation.
- b – Second operand for + operation.
31. toBinaryString()
The binaryString() method returns String representation of the unsigned int argument. This conversion happens in base 2 (binary number system).
Syntax:
public static String toBinaryString(int i)
Parameters:
i
– An int value to be converted to a String.
32. toHexString()
The toHexString() method returns String representation of the unsigned int argument. This conversion happens in base 16 (hexadecimal number system).
Syntax:
public static String toHexString(int i)
Parameters:
i
– An int value to be converted to a String in base 16.
33. toOctalString()
The toOctalString() method returns String representation of the unsigned int argument in base 8.
Syntax:
public static String toOctalString(int i)
Parameters:
i
– An int value to be converted to a String in base 8.
34. toString()
The toString() method returns String object representing this Integer value. The value is converted to a signed decimal representation and then converted to a String.
public String toString()
An overloaded toString() method with one argument: The given int value is converted to String using radix value as 10.
public String toString(int i)
An overloaded toString() method with two arguments: The given int value is converted to String using specified radix value.
public String toString(int i, int radix)
35. toUnsignedLong()
The toUnsignedLong() method converts the given int value to long using unsigned conversion.
Syntax:
public static long toUnsignedLong(int i)
Parameters:
i
– An int value whose unsigned long is to determined.
36. toUnsignedString()
The toUnsignedString() method returns string representation of the given unsigned decimal value.
Syntax:
public static String toUnsignedString(int i) //base 10 public static String toUnsignedString(String s, int radix) throws NumberFormatException //base given radix
Parameters:
i
– An int value to be parsed to a string.s
– A string containing integer representation to be parsed.radix
– The radix used in parsing.
37. valueOf()
The valueOf() method returns Integer object representing given int value.
Syntax:
public static Integer valueOf(int i)
Another overloaded method with String argument: It returns Integer value represented by the specified string value.
public static Integer valueOf(String s) throws NumberFormatException
Another overloaded method with two arguments: It returns Integer value represented by the specified string value and specified radix value.
public static Integer valueOf(String s, int radix) throws NumberFormatException
Parameters:
i
– An int value.s
– A string to be parsed to an Integer.radix
– The radix used in parsing.
Conclusion
In this article, we have discussed all the methods of Integer class with syntax and parameters. To read more such in depth guides on other wrapper classes refer this tutorial: Wrapper classes in Java.
Reference: