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

Méthode Java Float class shortValue() avec exemple

Méthode shortValue() de la classe flottante

  • méthode shortValue() est disponible dans le package java.lang.
  • méthode shortValue() est utilisé pour renvoyer la valeur indiquée par cet objet Float converti en type short (par cast).
  • méthode shortValue() 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 shortValue() ne lève pas d'exception au moment de la conversion de float en short.

Syntaxe :

    public short shortValue();

Paramètre(s) :

  • Il n'accepte aucun paramètre.

Valeur renvoyée :

Le type de retour de cette méthode est short, elle retourne une valeur convertie (de type float à short) représentée par cet objet Float.

Exemple :

// Java program to demonstrate the example 
// of shortValue() method of Float class

public class ShortValueOfFloatClass {
    public static void main(String[] args) {
        // Variables initialization
        float f1 = 18.20f;
        float f2 = 19.20f;

        // It returns float value denoted by this Float ob1 object
        // and converted to a short by calling ob1.shortValue()
        Float ob1 = new Float(f1);

        // Display ob1 result
        System.out.println("ob1.shortValue(): " + ob1.shortValue());

        // It returns float value denoted by this Float ob2 object
        // and converted to a short by calling ob2.shortValue()
        Float ob2 = new Float(f2);

        // Display ob2 result
        System.out.println("ob2.shortValue(): " + ob2.shortValue());
    }
}

Sortie

ob1.shortValue(): 18
ob2.shortValue(): 19

Balise Java