StringBuffer에 관한 면접 문제

1313 단어 면접 시험
public class Test {
	public static StringBuffer doSomething(StringBuffer buff) {
		buff = new StringBuffer();
		buff.append("Hello World");
		System.out.println(buff);  //Hello World
		return buff;
	}

	public static void main(String[] args) {
		StringBuffer buff = new StringBuffer();
		buff.append("Hello");
		System.out.println(doSomething(buff));  //Hello World
		System.out.println(buff);    //Hello
	}
}

 
 
 
2: 현재 DoSomething () 방법에서 버프 = new String 버퍼 () 를 제거합니다.
public class Test {
	public static StringBuffer doSomething(StringBuffer buff) {

		buff.append("Hello World");
		System.out.println(buff);  
		return buff;
	}

	public static void main(String[] args) {
		StringBuffer buff = new StringBuffer();
		buff.append("Hello");
		System.out.println(doSomething(buff)); 
		System.out.println(buff);   
	}
}

 
결과:
HelloHello WorldHelloHello WorldHelloHello World
 
기술 요점: 1에 StringBuffer 대상이 전송되었지만main에 buff 인용을 DoSomething ()에 부여했지만 방법 내부에서 다른 대상(메모리)을 다시 가리킨다.그래서 원래 게 효과가 없어요!
2는 항상 버프 대상입니다
 

좋은 웹페이지 즐겨찾기