为了将原始类型值转换为Java中的字符串,请使用valueOf()
方法。假设我们要将以下双精度原始类型转换为字符串。
double val4 = 8.34D;
为此,请使用valueOf()
方法。它将其转换为字符串。
String str4 = String.valueOf(val4);
public class Demo { public static void main(String[] args) { int val1 = 5; char val2 = 'z'; float val3 = 9.56F; double val4 = 8.34D; System.out.println("Integer: "+val1); System.out.println("Character: "+val2); System.out.println("Float: "+val3); System.out.println("Double: "+val4); String str1 = String.valueOf(val1); String str2 = String.valueOf(val2); String str3 = String.valueOf(val3); String str4 = String.valueOf(val4); System.out.println("\nConverted to string..."); System.out.println(str1); System.out.println(str2); System.out.println(str3); System.out.println(str4); } }
输出结果
Integer: 5 Character: z Float: 9.56 Double: 8.34 Converted to string... 5 z 9.56 8.34