English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Convertir Short en type de données primitive en nombre en Java

Pour convertir Short en type de données primitive numérique, utilisez les méthodes suivantes-

byteValue()
shortValue()
intValue()
longValue()
floatValue()

Tout d'abord, déclarons une Short.

Short shortObj = new Short("40);

Maintenant, regardons un exemple simple de comment le convertir en type long.

long val5 = shortObj.longValue();
System.out.println("Long : "+val5);

De la même manière, il peut être converti en d'autres types de données primitives, comme montré dans l'exemple complet ci-dessous.

Exemple

public class Demo {
   public static void main(String []args) {
      Short shortObj = new Short("40);
      byte val1 = shortObj.byteValue();
      System.out.println("Byte : "+val1);
      int val2 = shortObj.intValue();
      System.out.println("Int : "+val2);
      float val3 = shortObj.floatValue();
      System.out.println("Float : "+val3);
      double val4 = shortObj.doubleValue();
      System.out.println("Double : "+val4);
      long val5 = shortObj.longValue();
      System.out.println("Long : "+val5);
   }
}

Résultat de la sortie

Byte : 40
Int : 40
Float : 40.0
Double : 40.0
Long : 40