Open In App

Java.lang.Integer class and its methods

Last Updated : 11 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.

JAVA




// Java code explaining the Integer Class methods
// bitcount(), toBinaryString(), toHexString(), toOctalString(), parse__()
import java.lang.*;
public class NewClass
{
    public static void main(String args[])
    {
        int x = 15, count1, y = 128, count2;
 
        // Use of toBinaryString() method
        System.out.println("Binary string of 16 : "
                           + Integer.toBinaryString(x));
        System.out.println("Binary string of 100 : "
                           + Integer.toBinaryString(y));
 
        // Use of bitCount() method
        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);
 
        // Use of toHexString() method
        System.out.println("\nHexadecimal string of 16 : "
                           + Integer.toHexString(x));
        System.out.println("Hexadecimal string of 100 : "
                           + Integer.toHexString(y));
        System.out.println("");
 
        // Use of toOctalString() method
        System.out.println("Octal string of 16 : "
                           + Integer.toOctalString(x));
        System.out.println("Octal string of 100 : "
                           + Integer.toOctalString(y) + "\n");
 
        // Use of parseInt() method
        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

  • 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.

JAVA




// Java program explaining Integer class methods
// hashcode(), lowestOneBit(), highestOneBit()
import java.lang.*;
public class NewClass
{
    public static void main(String[] args)
    {
        // Use of incrementExact() method
        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));
 
        // Use of lowestOneBit() method
        // Here it considers 00010 i.e. 2
        System.out.println("lowestOneBit of 30 : "
                           + Integer.lowestOneBit(f1));
 
 
        // Use of highestOneBit() method
        // Here it considers 10000 i.e. 16
        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

  • 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

JAVA




// Java program explaining Integer class methods
// numberOfTrailingZeros(), numberOfLeadingZeros(), reverse()
import java.lang.*;
 
// Driver Class
public class NewClass
{
      // Main Method
    public static void main(String[] args)
    {
        int f1 = 30;
 
        // Binary representation of int arg for your understanding
        System.out.println("Binary representation of 30 : "
                           + Integer.toBinaryString(f1));
 
        // Use of numberOfTrailingZeros() method
        // No. of zeros following 1 in 00010 = 1
        System.out.println("\nNo. Of Trailing Zeros : "
                           + Integer.numberOfTrailingZeros(f1));
 
        // Use of highestOneBit() method
        // No. of zeros following 1 in 10000 i.e. 32 - 5 = 27
        System.out.println("\nNo. Of Leading Zeros : "
                           + Integer.numberOfLeadingZeros(f1));
 
        // Use of Reverse() method
        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


Similar Reads

java.lang.Character class methods | Set 1
[caption id="attachment_151090" align="aligncenter" width="787"] lang.Character class wraps the value of a primitive data type - char to an object of datatype char and this object contains a single field having the data type - char. This class provides no. of methods regarding character manipulations like converting them from lowercase to uppercase
7 min read
java.lang.Character class - methods | Set 2
[caption id="attachment_151396" align="aligncenter" width="755"] lang.Character methods[/caption] java.lang.Character class - methods | Set 1 Following methods of Character class are discussed here : toUpperCase() : java.lang.toUpperCase(char arg) method converts given character in argument to its Upper case based on Unicode values. Syntax : public
4 min read
java.lang.Boolean class methods
[caption width="800"]Boolean class methods.[/caption] About : java.lang.Boolean class wraps primitive type boolean value in an object.Class Declaration public final class Boolean extends Object implements Serializable, Comparable Constructors : Boolean(boolean val) : Assigning Boolean object representing the val argument. Boolean(String str) : Assi
6 min read
Java.lang.Class class in Java | Set 1
Java provides a class with name Class in java.lang package. Instances of the class Class represent classes and interfaces in a running Java application. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects. It has no public constructor. Class objects are cons
31 min read
Java.lang.Class class in Java | Set 2
Java.lang.Class class in Java | Set 1 More methods: 1. int getModifiers() : This method returns the Java language modifiers for this class or interface, encoded in an integer. The modifiers consist of the Java Virtual Machine's constants for public, protected, private, final, static, abstract and interface. These modifiers are already decoded in Mo
18 min read
Java lang.Integer.toBinaryString() method
The java.lang.Integer.toBinaryString() method returns a string representation of the integer argument as an unsigned integer in base 2. It accepts an argument in Int data-type and returns the corresponding binary string. Syntax: public static String toBinaryString(int num) Parameter : The function accepts a single mandatory parameter num num - This
1 min read
Java lang Integer.toHexString() Method with Examples
The Java.lang.Integer.toHexString() is a built-in function in Java which returns a string representation of the integer argument as an unsigned integer in base 16. The function accepts a single parameter as an argument in Integer data-type. Syntax : public static String toHexString(int num) Parameter : The function accepts a single mandatory parame
3 min read
Java.lang.StrictMath class in Java | Set 2
Java.lang.StrictMath Class in Java | Set 1More methods of java.lang.StrictMath class 13. exp() : java.lang.StrictMath.exp(double arg) method returns the Euler’s number raised to the power of double argument. Important cases: Result is NaN, if argument is NaN.Result is +ve infinity, if the argument is +ve infinity.Result is +ve zero, if argument is
6 min read
java.lang.instrument.ClassDefinition Class in Java
This class is used to bind together the supplied class and class file bytes in a single ClassDefinition object. These class provide methods to extract information about the type of class and class file bytes of an object. This class is a subclass of java.lang.Object class. Class declaration: public final class ClassDefinition extends ObjectConstruc
2 min read
java.lang.reflect.Proxy Class in Java
A proxy class is present in java.lang package. A proxy class has certain methods which are used for creating dynamic proxy classes and instances, and all the classes created by those methods act as subclasses for this proxy class. Class declaration: public class Proxy extends Object implements SerializableFields: protected InvocationHandler hIt han
4 min read
Practice Tags :