VOOZH about

URL: https://www.geeksforgeeks.org/java/java-lang-integer-class-methods/

⇱ Java.lang.Integer class and its methods - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java.lang.Integer class and its methods

Last Updated : 23 Jul, 2025

java.lang.Integer wraps integer data type to an object containing a single field having datatype is int. 

Constructors :

  • Integer (int arg) : Constructs integer object representing an int value.
  • Integer (String arg) : Constructs string object representing a string value.


Integer class methods: 

  • toBinaryString() : java.lang.Integer.toBinaryString() method converts the integer value of argument its Binary representation as a string. 
    Syntax
public static String toBinaryString(int arg)
Parameters
arg : integer argument whose Binary representation we want
Return
Binary representation of the argument.
  • bitcount() : java.lang.Integer.bitCount()
    method converts the integer value of argument to Binary string and then returns the no. of 1's present in it. 
    Syntax
public static int bitCount(int arg)
Parameters
arg : integer argument whose no. of 1's bit we want
Return
no. of 1's bit present in the argument.
  • toHexString() : java.lang.Integer.toHexString() method converts the integer value of argument its Hexadecimal representation as a string. 
    Syntax
public static String toHexString(int arg)
Parameters
arg : integer argument whose Hexadecimal representation we want
Return
Hexadecimal representation of the argument.
  • toOctalString() : java.lang.Integer.toHexString() method converts the integer value of argument its Hexadecimal representation as a string. 
    Syntax
public static String toHexString(int arg)
Parameters
arg : integer argument whose Hexadecimal representation we want
Return
Hexadecimal representation of the argument.
  • parsedatatype() : java.lang.Integer.parse__() method returns primitive data type of the argumented String value. 
    Radix (r) means numbering format used is at base 'r' to the string. 
    Syntax
public static int parseInt(String arg)
or
public static int parseInt(String arg, int r)
Parameters
arg : argument passed
r : radix
Return
primitive data type of the argumented String value.

Output:  

Binary string of 16 : 1111
Binary string of 100 : 10000000
1's bit present in 16 : 4
1's bit present in 100 : 1
Hexadecimal string of 16 : f
Hexadecimal string of 100 : 80
Octal string of 16 : 17
Octal string of 100 : 200
34
13
54.0
  • hashCode() : java.lang.Integer.hashCode() method returns the hashCode value of the argument passed. 
    Syntax:
public int hashCode(arg)
Parameters:
arg - the argument whose hashCode value we need
Returns:
hashCode value of arg
  • lowestOneBit() : java.lang.Integer.lowestOneBit() method first convert int to Binary, then it looks for set(1) bit present at lowest position then it reset rest of the bits 
    e.g arg = 36 
    It,s Binary Representation = 0010 0100 
    It considers lowest bit(at 3) and now reset rest of the bits i.e. 0000 0100 
    so result = 0100 i.e. 4 
    Syntax:
public static int lowestOneBit(int arg)
Parameters:
arg - argument passed
Returns:
integer value by only considering lowest 1 bit in the argument.
  • highestOneBit() : java.lang.Integer.highestOneBit() method first convert int to Binary, then it looks for set(1) bit present at lowest position then it reset rest of the bits 
    e.g arg = 36 
    It,s Binary Representation = 0010 0100 
    It considers highest bit(at 6) and now reset rest of the bits i.e. 0001 0000 
    so result = 10000 i.e. 32 
    Syntax:
public static int highestOneBit(int arg)
Parameters:
arg - argument passed
Returns:
integer value by only considering highest 1 bit in the argument.

Output:  

HashCode value of f1 : 30
HashCode value of f2 : -56
Binary representation of 30 : 11110
lowestOneBit of 30 : 2
highestOneBit of 30 : 16
  • numberOfTrailingZeros() : java.lang.Integer.numberOfTrailingZeros() method converts int value to Binary then considers the lowest one bit and return no. of zero bits following it. 
    e.g arg = 36 
    It,s Binary Representation = 0010 0100 
    It considers highest bit(at 6) i.e. 0001 0000 
    so result = 4 
    Syntax:
public static int numberOfTrailingZeros(int arg)
Parameters:
arg - the argument
Returns:
Number of zero bits following the 1 bit at lowest position
  • numberOfLeadingZeros() : java.lang.Integer.numberOfLeadingZeros() method converts int value to Binary then considers the highest one bit and return no. of zero bits preceding it. 
    e.g arg = 36 
    It,s Binary Representation = 0010 0100 
    It considers highest bit(at 6) i.e. 0010 0000 
    so result = 32 - 6 i.e. 26 
    Syntax:
public static int numberOfLeadingZeros(int arg)
Parameters:
arg - the argument
Returns:
Number of zero bits preceding the 1 bit at highest position

reverse() : java.lang.Integer.reverse() method first find 2's complement of the argument passed and reverses the order of bits in the 2's complement. 

Syntax:

public static int reverse(int arg)

Parameters: arg - the argument

Returns: int with reverse order of bits in 2's complement of the passed argument

Output: 

Binary representation of 30 : 11110
No. Of Trailing Zeros : 1
No. Of Leading Zeros : 27
Reverse : 2013265920
Comment
Article Tags: