String和StringBuffer转换
/*String 转为StringBuffer*/ public class StringBufferDemo { public static void main(String[] args) { //String --> StringBuffer String string = "Hello"; //构造方法转换 StringBuffer buffer = new StringBuffer(string); System.out.println(buffer); //append()方法 StringBuffer buffer2 = new StringBuffer(); buffer2.append(string); System.out.println("buffer2:"+buffer2); } }
/*StringBuffer转String*/ public class StringBufferDemo { public static void main(String[] args) { //StringBuffer --> String StringBuffer buffer = new StringBuffer("Java"); //构造方法转换 String string = new String(buffer); System.out.println("string:"+string); //toSting()方法 //通过toString方法 String string2 = buffer.toString(); System.out.println("string2 : "+string2); } }