Skip to Content

Convert Long to numeric primitive data types example

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