Convert Java Primitive to String

Convert Java Primitive to String Example describes about Converting a Primitive Type Value To String In Java.

There are two ways, you can able to convert java primitive type value into a string

  • Use String.valueOf() method of String class
  • Using string concatenation operator '+'.

Convert Java Primitive to String

public class ConvertPrimitiveToString {

 
public static void main(String[] args) {

   
String str = String.valueOf(true);

    str = String.valueOf
((byte) 0x11);
    str = String.valueOf
((byte) 0xFF);
    str = String.valueOf
('a');
    str = String.valueOf
((short) 123);
    str = String.valueOf
(123);
    str = String.valueOf
(123L);
    str = String.valueOf
(1.23F);
    str = String.valueOf
(1.23D);

   
// Use +
   
str = "" + true;
    str =
"" + ((byte) 0x12);
    str =
"" + ((byte) 0xFF);
    str =
"" + 'a';
    str =
"" + ((short) 123);
    str =
"" + 111;
    str =
"" + 111L;
    str =
"" + 1.11F;
    str =
"" + 1.11D;
 
}
}








Your email address will not be published. Required fields are marked *