Java >> Tutoriel Java >  >> Tag >> class

Méthode Java Long class toBinaryString() avec exemple

Méthode classe longue toBinaryString()

  • méthode toBinaryString() est disponible dans le package java.lang.
  • méthode toBinaryString() est utilisé pour représenter une chaîne binaire du paramètre donné [valeur] de type long sous la forme d'un entier non signé en binaire (base 2).
  • méthode toBinaryString() est une méthode statique, elle est également accessible avec le nom de la classe et si nous essayons d'accéder à la méthode avec l'objet de la classe, nous n'obtiendrons pas non plus d'erreur.
  • méthode toBinaryString() ne lève pas d'exception au moment de la conversion d'une chaîne longue en une chaîne binaire.

Syntaxe :

    public static String ToBinaryString(long value);

Paramètre(s) :

  • valeur longue - représente la valeur longue à convertir.

Valeur renvoyée :

Le type de retour de cette méthode est String, elle renvoie la chaîne binaire du paramètre donné qui représente la valeur longue non signée.

Exemple :

// 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);
    }
}

Sortie

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

Balise Java