这些声明之间有何不同?
String s="MY PROFESSION"; char c[]="MY PROFESSION";
每种情况下的内存分配如何?
要更正编译错误,请使用以下char[]语句之一替换
char[]
String s = "MY PROFESSION"; char c1[] = "MY PROFESSION".toCharArray(); char c2[] = { 'M', 'Y', ' ', 'P', 'R', 'O', 'F', 'E', 'S', 'S', 'I', 'O', 'N' }; StringBuilder sb = new StringBuilder("MY PROFESSION"); StringBuffer sbu = new StringBuffer("MY PROFESSION");
下一节将以上陈述相互比较
String s="MY PROFESSION";
s
String
char c1[]="MY PROFESSION".toCharArray(); char c2[]={'M', 'Y', ' ', 'P', 'R', 'O', 'F', 'E', 'S', 'S', 'I', 'O', 'N'};
c1
System.arraycopy
c2
Array
c2[0]='B'
StringBuilder sb = new StringBuilder("MY PROFESSION"); StringBuffer sbu = new StringBuffer("MY PROFESSION");
sb
sbu
sb.replace(0, 1, 'B');
sb.append( '!');
StringBuffer
StringBuilder