JAVA String类和StringBuffer类 的区别
String类和StringBuffer类
位于java.lang包中,这个包中的类使用时不用导入
String类一旦初始化就不可以改变,而stringbuffer则可以。它用于封装内容可变的字符串。它可以使用tostring()转换成string字符串。
String x=”a”+4+”c”编译时等效于String x=new StringBuffer().append(“a”).append(4).append(“c”).toString();
字符串常量是一种特殊的匿名对象,String s1=”hello”;String s2=”hello”;则s1==s2;因为他们指向同一个匿名对象。
如果String s1=new String(“hello”);String s2=new String(“hello”);则s1!=s2;
/*逐行读取键盘输入,直到输入为“bye”时,结束程序
注:对于回车换行,在windows下面,有'\r'和'\n'两个,而unix下面只有'\n',但是写程序的时候都要把他区分开*/
(2) StringBuffer sb=new StringBuffer(); For(int j=0;j { Sb.append(‘*’); }