Méthode toBinaryString de l'entier Java

JavaJavaBeginner
Pratiquer maintenant

💡 Ce tutoriel est traduit par l'IA à partir de la version anglaise. Pour voir la version originale, vous pouvez cliquer ici

Introduction

Dans ce laboratoire, nous allons apprendre à utiliser la méthode toBinaryString() de la classe Integer en Java. Cette méthode est utilisée pour convertir un nombre décimal en son équivalent binaire.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("Java")) -.-> java/BasicSyntaxGroup(["Basic Syntax"]) java(("Java")) -.-> java/StringManipulationGroup(["String Manipulation"]) java(("Java")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["Object-Oriented and Advanced Concepts"]) java(("Java")) -.-> java/SystemandDataProcessingGroup(["System and Data Processing"]) java/BasicSyntaxGroup -.-> java/variables("Variables") java/BasicSyntaxGroup -.-> java/output("Output") java/BasicSyntaxGroup -.-> java/type_casting("Type Casting") java/StringManipulationGroup -.-> java/strings("Strings") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("Packages / API") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("Wrapper Classes") java/SystemandDataProcessingGroup -.-> java/math_methods("Math Methods") java/SystemandDataProcessingGroup -.-> java/string_methods("String Methods") subgraph Lab Skills java/variables -.-> lab-117750{{"Méthode toBinaryString de l'entier Java"}} java/output -.-> lab-117750{{"Méthode toBinaryString de l'entier Java"}} java/type_casting -.-> lab-117750{{"Méthode toBinaryString de l'entier Java"}} java/strings -.-> lab-117750{{"Méthode toBinaryString de l'entier Java"}} java/packages_api -.-> lab-117750{{"Méthode toBinaryString de l'entier Java"}} java/wrapper_classes -.-> lab-117750{{"Méthode toBinaryString de l'entier Java"}} java/math_methods -.-> lab-117750{{"Méthode toBinaryString de l'entier Java"}} java/string_methods -.-> lab-117750{{"Méthode toBinaryString de l'entier Java"}} end

Importez le package java.lang.Integer

Pour utiliser la classe Integer, nous devons importer le package java.lang.Integer.

import java.lang.Integer;

Utilisez la méthode toBinaryString()

La méthode toBinaryString() prend un entier en entrée et renvoie son équivalent binaire sous forme de chaîne de caractères. Voyons un exemple où nous allons convertir l'entier "10" en son équivalent binaire.

int decimalValue = 10;
String binaryValue = Integer.toBinaryString(decimalValue);

Affichez la sortie

Maintenant que nous avons l'équivalent binaire de la valeur décimale, affichons-la dans la console.

System.out.println("La représentation binaire de " + decimalValue + " est : " + binaryValue);

Récapitulatif

Dans ce laboratoire, nous avons appris à propos de la méthode toBinaryString() de la classe Integer en Java. Nous avons appris comment importer le package, utiliser la méthode et afficher l'équivalent binaire. Nous avons également appris comment tester le code avec différentes valeurs décimales.