Skip to Content

Convert Short to numeric primitive data types example

  1. /*
  2.   Convert Short to numeric primitive data types example
  3.   This example shows how a Short object can be converted into below given numeric
  4.   data types
  5.   - Convert Short to byte
  6.   - Convert Short to short
  7.   - Convert Short to int
  8.   - Convert Short to float
  9.   - Convert Short to double
  10.   - Convert Short to long
  11. */
  12.  
  13. public class ShortToNumericPrimitiveTypesExample {
  14.  
  15.   public static void main(String[] args) {
  16.     Short sObj = new Short("10");
  17.     //use byteValue method of Short class to convert it into byte type.
  18.     byte b = sObj.byteValue();
  19.     System.out.println(b);
  20.    
  21.     //use shortValue method of Short class to convert it into short type.
  22.     short s = sObj.shortValue();
  23.     System.out.println(s);
  24.    
  25.     //use intValue method of Short class to convert it into int type.
  26.     int i = sObj.intValue();
  27.     System.out.println(i);
  28.    
  29.     //use floatValue method of Short class to convert it into float type.
  30.     float f = sObj.floatValue();
  31.     System.out.println(f);
  32.  
  33.     //use doubleValue method of Short class to convert it into double type.
  34.     double d = sObj.doubleValue();
  35.     System.out.println(d);
  36.    
  37.     //use longValue method of Short class to convert it into long type.
  38.     long l = sObj.longValue();
  39.     System.out.println(l);
  40.  
  41.   }
  42. }
  43.  
  44. /*
  45. Output of the program would be :
  46. 10
  47. 10
  48. 10
  49. 10.0
  50. 10.0
  51. 10
  52. */