Java Data Type How to - Convert primitive data types to String








Question

We would like to know how to convert primitive data types to String.

Answer

//  ww w . j  ava 2 s .c  o m

//The String class has an overloaded valueOf()  static method to 
//get the string representation of the values of any primitive data type or any object. For example,

public class Main {
  public static void main(String[] args) {
    String s1 = String.valueOf('C'); // s1 has "C"
    String s2 = String.valueOf("10"); // s2 has "10"
    String s3 = String.valueOf(true); // s3 has "true"
    String s4 = String.valueOf(2014); // s4 has "2014"
  }
}