Java - Integer Class toHexString() Method

Integer class toHexString() method: Here, we are going to learn about the toHexString() method of Integer class with its syntax and example. By Preeti Jain Last updated : March 18, 2024

Integer class toHexString() method

  • toHexString() method is available in java.lang package.
  • toHexString() method is used to represent a hexadecimal string of the given parameter [value] of integer type as an unsigned integer in base 16.
  • toHexString() method is a static method, it is accessible with the class name too and if we try to access the method with the class object then also we will not get an error.
  • toHexString() method does not throw an exception at the time of conversion from integer to Hexadecimal string.

Syntax

public static String toHexString (int value);

Parameters

  • int value – represents the integer value to be converted.

Return Value

The return type of this method is int, it returns the hexadecimal string of the given parameter that represent the unsigned integer value.

Example

// Java program to demonstrate the example 
// of toHexString (int value) method of Integer class

public class ToHexStringOfIntegerClass {
    public static void main(String[] args) {
        // Variables initialization
        int i1 = 10;
        int i2 = 20;
        int i3 = 30;
        int i4 = Integer.MAX_VALUE;
        int i5 = Integer.MIN_VALUE;

        // Integer instance creation
        Integer value = new Integer(i1);

        // It represents hexadecimal string of the given
        // integer type i2 argument
        String s = value.toHexString(i2);

        // Display Hexadecimal String Representation
        System.out.println("value.toHexString(i2): " + s);

        // It represents hexadecimal string of the given
        // integer type i3 argument
        s = value.toHexString(i3);

        // Display Hexadecimal String Representation
        System.out.println("value.toHexString(i3): " + s);

        // It represents hexadecimal string of the given
        // integer type i4 argument
        s = value.toHexString(i4);

        // Display Hexadecimal String Representation
        System.out.println("value.toHexString(i4): " + s);

        // It represents hexadecimal string of the given
        // integer type i5 argument
        s = value.toHexString(i5);

        // Display Hexadecimal String Representation
        System.out.println("value.toHexString(i5): " + s);
    }
}

Output

value.toHexString(i2): 14
value.toHexString(i3): 1e
value.toHexString(i4): 7fffffff
value.toHexString(i5): 80000000

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.