Java.lang.Integer class and its methods (original) (raw)

Last Updated : 11 Mar, 2024

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

**Constructors :

Integer class methods:

public static String toBinaryString(int arg)
**Parameters
arg : integer argument whose Binary representation we want
**Return
Binary representation of the argument.

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.

public static String toHexString(int arg)
**Parameters
arg : integer argument whose Hexadecimal representation we want
**Return
Hexadecimal representation of the argument.

public static String toHexString(int arg)
**Parameters
arg : integer argument whose Hexadecimal representation we want
**Return
Hexadecimal representation of the argument.

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.

JAVA

import java.lang.*;

public class NewClass

{

`` public static void main(String args[])

`` {

`` int x = 15 , count1, y = 128 , count2;

`` System.out.println( "Binary string of 16 : "

`` + Integer.toBinaryString(x));

`` System.out.println( "Binary string of 100 : "

`` + Integer.toBinaryString(y));

`` count1 = Integer.bitCount(x);

`` System.out.println( "\n 1's bit present in 16 : " +count1);

`` count2 = Integer.bitCount(y);

`` System.out.println( " 1's bit present in 100 : " +count2);

`` System.out.println( "\nHexadecimal string of 16 : "

`` + Integer.toHexString(x));

`` System.out.println( "Hexadecimal string of 100 : "

`` + Integer.toHexString(y));

`` System.out.println( "" );

`` System.out.println( "Octal string of 16 : "

`` + Integer.toOctalString(x));

`` System.out.println( "Octal string of 100 : "

`` + Integer.toOctalString(y) + "\n" );

`` int i1 =Integer.parseInt( "34" );

`` int i2 = Integer.parseInt( "15" , 8 );

`` double d = Double.parseDouble( "54" );

`` System.out.println(i1);

`` System.out.println(i2);

`` System.out.println(d);

`` }

}

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

public int hashCode(arg)
**Parameters:
arg - the argument whose hashCode value we need
**Returns:
hashCode value of arg

public static int lowestOneBit(int arg)
**Parameters:
arg - argument passed
**Returns:
integer value by only considering lowest 1 bit in the argument.

public static int highestOneBit(int arg)
**Parameters:
arg - argument passed
**Returns:
integer value by only considering highest 1 bit in the argument.

JAVA

import java.lang.*;

public class NewClass

{

`` public static void main(String[] args)

`` {

`` int f1 = 30 , f2 = - 56 ;

`` f1 = Integer.hashCode(f1);

`` System.out.println( "HashCode value of f1 : " +f1);

`` f2 = Integer.hashCode(f2);

`` System.out.println( "HashCode value of f2 : " +f2);

`` System.out.println( "\nBinary representation of 30 : "

`` + Integer.toBinaryString(f1));

`` System.out.println( "lowestOneBit of 30 : "

`` + Integer.lowestOneBit(f1));

`` System.out.println( "highestOneBit of 30 : "

`` + Integer.highestOneBit(f1));

`` }

}

Output:

HashCode value of f1 : 30
HashCode value of f2 : -56
Binary representation of 30 : 11110
lowestOneBit of 30 : 2
highestOneBit of 30 : 16

public static int numberOfTrailingZeros(int arg)
**Parameters:
arg - the argument
**Returns:
Number of zero bits following the 1 bit at lowest position

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

JAVA

import java.lang.*;

public class NewClass

{

`` public static void main(String[] args)

`` {

`` int f1 = 30 ;

`` System.out.println( "Binary representation of 30 : "

`` + Integer.toBinaryString(f1));

`` System.out.println( "\nNo. Of Trailing Zeros : "

`` + Integer.numberOfTrailingZeros(f1));

`` System.out.println( "\nNo. Of Leading Zeros : "

`` + Integer.numberOfLeadingZeros(f1));

`` System.out.println( "\nReverse : " + Integer.reverse(f1));

`` }

}

Output:

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