1.StringBuilder转换为String
public string toString():通过toString就可以实现把StringBuilder转换为String
2.String转换为StringBuilder
public StringBuilder(String s):通过构造方法就可以实现把String转换为StringBuilder
package com.ithema_15;
/*
1.StringBuilder转换为String
public string toString():通过toString就可以实现把StringBuilder转换为String
2.String转换为StringBuilder
public StringBuilder(String s):通过构造方法就可以实现把String转换为StringBuilder
*/
public class StringBuilderDemo03 {
public static void main(String[] args){
/*
//StringBuilder转换为String
StringBuilder sb = new StringBuilder();
sb.append("hello");
// String s = sb; //这个是错误的做法
//public string toString():通过toString就可以实现把StringBuilder转换为String
String s = sb.toString();
System.out.println(s);
*/
//String转换为StringBuilder
String s = "hello";
// StringBuilder sb = s; //这个方法是错误的
//public StringBuilder(String s):通过构造方法就可以实现把String转换为StringBuilder
StringBuilder sb = new StringBuilder(s);
System.out.println(sb);
}
}