String str = "我是 java-er.com";
System.out.println(str);
String str = new String("我是 java-er.com");
System.out.println(str);
3.用数组构建
char[] arr = {‘J‘,‘a‘,‘v‘,‘a‘};
String arrString = new String(arr);
System.out.println(arrString);
二、String?格式化
输出格式化数字可以使用 printf() 和 format() 方法。
float f = 2.4f;
int i = 5;
String s = "字符串";
System.out.printf("浮点型变量的值为 " +
"%f, 整型变量的值为 " +
" %d, 字符串变量的值为 " +
"is %s", f, i, s);
?
String fs = String.format("浮点型变量的值为 " +
"%f, 整型变量的值为 " +
" %d, 字符串变量的值为 " +
"is %s", f, i, s);
?
System.out.println("\n我可以再次打印:" + fs);
输出
浮点型变量的值为 2.400000, 整型变量的值为 5, 字符串变量的值为 is 字符串
我可以再次打印:浮点型变量的值为 2.400000, 整型变量的值为 5, 字符串变量的值为 is 字符串
https://java-er.com/blog/java-string-study/
java StringBuffer,StringBuilder,String自身连接效率对比
原文:https://blog.51cto.com/yuexiaosheng/2473049