Java String valueOf Example

  1. /*
  2. Java String valueOf example.
  3. This Java String valueOf example describes how various java primitives and Object
  4. are converted to Java String object using String valueOf method.
  5. */
  6.  
  7. public class JavaStringValueOfExample{
  8.  
  9. public static void main(String args[]){
  10.  
  11. /*
  12.   Java String class defines following methods to convert various Java primitives to
  13.   Java String object.
  14.   1) static String valueOf(int i)
  15.   Converts argument int to String and returns new String object representing
  16.   argument int.
  17.   2) static String valueOf(float f)
  18.   Converts argument float to String and returns new String object representing
  19.   argument float.
  20.   3) static String valueOf(long l)
  21.   Converts argument long to String and returns new String object representing
  22.   argument long.
  23.   4) static String valueOf(double i)
  24.   Converts argument double to String and returns new String object representing
  25.   argument double.
  26.   5) static String valueOf(char c)
  27.   Converts argument char to String and returns new String object representing
  28.   argument char.
  29.   6) static String valueOf(boolean b)
  30.   Converts argument boolean to String and returns new String object representing
  31.   argument boolean.
  32.   7) static String valueOf(Object o)
  33.   Converts argument Object to String and returns new String object representing
  34.   argument Object.
  35.   */
  36.  
  37. int i=10;
  38. float f = 10.0f;
  39. long l = 10;
  40. double d=10.0d;
  41. char c='a';
  42. boolean b = true;
  43. Object o = new String("Hello World");
  44.  
  45. /* convert int to String */
  46. System.out.println( String.valueOf(i) );
  47. /* convert float to String */
  48. System.out.println( String.valueOf(f) );
  49. /* convert long to String */
  50. System.out.println( String.valueOf(l) );
  51. /* convert double to String */
  52. System.out.println( String.valueOf(d) );
  53. /* convert char to String */
  54. System.out.println( String.valueOf(c) );
  55. /* convert boolean to String */
  56. System.out.println( String.valueOf(b) );
  57. /* convert Object to String */
  58. System.out.println( String.valueOf(o) );
  59.  
  60. }
  61.  
  62. }
  63.  
  64. /*
  65. OUTPUT of the above given Java String valueOf Example would be :
  66. 10
  67. 10.0
  68. 10
  69. 10.0
  70. a
  71. true
  72. true
  73. Hello World
  74. */

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!