- public static void main(String args[]){
- String textString=new String("java");
- StringBuffer textBuffer=new StringBuffer("java");
- stringReplace(textString); //虽然这里调用了stringReplace()方法,但是并没有为textString赋值,所以textString的值仍然是"java"
- bufferReplace(textBuffer); //这里StringBuffer进行了append追加,其值为"javac"
- System.out.println(textString+textBuffer); //"java"+"javac"
- }
- }
- 这样就可以得到你想要的结果了:
- public class Test {
- public static String stringReplace(String text) {
- text = text.replace("j", "L");
- return text;
- }
- public static void bufferReplace(StringBuffer text) {
- text = text.append("c");
- }
- public static void main(String args[]) {
- String textString = new String("java");
- StringBuffer textBuffer = new StringBuffer("java");
- textString = stringReplace(textString);
- bufferReplace(textBuffer);
- System.out.println(textString + textBuffer);
- }
- }
复制代码 |