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

Méthode byteValue() de la classe Java Integer avec exemple

Méthode byteValue() de la classe Integer

  • méthode byteValue() est disponible dans le package java.lang.
  • méthode byteValue() est utilisé pour renvoyer la valeur indiquée par cet objet Integer converti en type byte (par cast).
  • méthode byteValue() est une méthode non statique, elle est accessible uniquement avec un objet de classe et si nous essayons d'accéder à la méthode avec le nom de la classe, nous obtiendrons une erreur.
  • méthode byteValue() ne lève pas d'exception au moment de la conversion d'entier en octet.

Syntaxe :

    public byte byteValue();

Paramètre(s) :

  • Il n'accepte aucun paramètre.

Valeur renvoyée :

Le type de retour de cette méthode est byte, elle retourne une valeur convertie (de type Integer en byte) représentée par cet objet Integer.

Exemple :

// Java program to demonstrate the example 
// of byteValue() method of Integer class

public class ByteValueOfIntegerClass {
    public static void main(String[] args) {
        // Variables initialization
        int i1 = 10;
        int i2 = 20;

        // It returns integer value denoted by this Integer value1 object
        // and converted to a byte by calling value1.byteValue()
        Integer value1 = new Integer(i1);

        // Display value1 result
        System.out.println("value1.byteValue(): " + value1.byteValue());

        // It returns integer value denoted by this Integer value2 object
        // and converted to a byte by calling value2.byteValue()
        Integer value2 = new Integer(i2);

        // Display value2 result
        System.out.println("value2.byteValue(): " + value2.byteValue());
    }
}

Sortie

value1.byteValue(): 10
value2.byteValue(): 20

Balise Java