Convert Java Integer object to Numeric primitive types

  1. /*
  2.   Convert Integer to numeric primitive data types example
  3.   This example shows how an Integer object can be converted into below given numeric
  4.   data types
  5.   - Convert Integer to byte
  6.   - Convert Integer to short
  7.   - Convert Integer to int
  8.   - Convert Integer to float
  9.   - Convert Integer to double
  10. */
  11.  
  12. public class IntegerToNumericPrimitiveTypesExample {
  13.  
  14. public static void main(String[] args) {
  15. Integer intObj = new Integer("10");
  16. //use byteValue method of Integer class to convert it into byte type.
  17. byte b = intObj.byteValue();
  18. System.out.println(b);
  19.  
  20. //use shortValue method of Integer class to convert it into short type.
  21. short s = intObj.shortValue();
  22. System.out.println(s);
  23.  
  24. //use intValue method of Integer class to convert it into int type.
  25. int i = intObj.intValue();
  26. System.out.println(i);
  27.  
  28. //use floatValue method of Integer class to convert it into float type.
  29. float f = intObj.floatValue();
  30. System.out.println(f);
  31.  
  32. //use doubleValue method of Integer class to convert it into double type.
  33. double d = intObj.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. */

more data type

more data type

hexadecimal
octal
etc.

Post new comment

To combat spam, please enter the code in the image.


Suggested Reading

Oracle Magazine
Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
Cost: FREE

View/Subscribe
NASA Tech Briefs
Features exclusive reports of innovations developed by NASA and its industry partners/contractors that can be applied to develop new/improved products and solve engineering or manufacturing problems.
Cost: FREE

View/Subscribe
FierceBiotech IT
Is a free, easy to read weekly email service that brings must read biotech IT news to senior biotech, pharma, and IT executives.
Cost: FREE

View/Subscribe
Simply SQL
Simply SQL is a practical step-by-step guide to writing SQL.
Cost: FREE

View/Subscribe
Simply JavaScript
Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
Cost: FREE

View/Subscribe
PCMag.com's What's New Now
Lance Ulanoff, Editor in Chief of the PC Magazine Network, brings you this twice-weekly roundup of the latest top tech stories, the best new product reviews, plus special offers from Ziff Davis and its partners.
Cost: FREE

View/Subscribe

Could not find what you are looking for? Search Java Examples




Feel Tired? Read Jokes & Inspirational Stories, Play Games!