Java - Long Class toBinaryString() Method

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

Long class toBinaryString() method

  • toBinaryString() method is available in java.lang package.
  • toBinaryString() method is used to represent a binary string of the given parameter [value] of long type as an unsigned integer in binary (base 2).
  • toBinaryString() 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.
  • toBinaryString() method does not throw an exception at the time of conversion from long to a binary string.

Syntax

    public static String ToBinaryString(long value);

Parameters

  • long value – represents the long value to be converted.

Return Value

The return type of this method is String, it returns the binary string of the given parameter that represent the unsigned long value.

Example

// Java program to demonstrate the example 
// of toBinaryString(long value) method of Long class

public class ToBinaryStringOfLongClass {
    public static void main(String[] args) {
        // Variables initialization
        long l1 = 10;
        long l2 = 20;
        long l3 = 30;
        long l4 = Long.MAX_VALUE;
        long l5 = Long.MIN_VALUE;

        // Long instance creation
        Long value = new Long(l1);

        // It represents binary string of the given
        // long type l2 argument
        String s = value.toBinaryString(l2);

        // Display Binary String Representation
        System.out.println("value.toBinaryString(l2): " + s);

        // It represents binary string of the given
        // long type l3 argument
        s = value.toBinaryString(l3);

        // Display Binary String Representation
        System.out.println("value.toBinaryString(l3): " + s);

        // It represents binary string of the given
        // long type l4 argument
        s = value.toBinaryString(l4);

        // Display Binary String Representation
        System.out.println("value.toBinaryString(l4): " + s);

        // It represents binary string of the given
        // long type l5 argument
        s = value.toBinaryString(l5);

        // Display Binary String Representation
        System.out.println("value.toBinaryString(l5): " + s);
    }
}

Output

value.toBinaryString(l2): 10100
value.toBinaryString(l3): 11110
value.toBinaryString(l4): 111111111111111111111111111111111111111111111111111111111111111
value.toBinaryString(l5): 1000000000000000000000000000000000000000000000000000000000000000

Comments and Discussions!

Load comments ↻





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