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는 항상 버프 대상입니다
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
LintCode - 순차적으로 숫자를 인쇄합니다.1에서 최대 N까지의 정수를 반복하는 방법으로 찾습니다. 예제 제시N = 1, 반환[1,2,3,4,5,6,7,8,9]. 제시N = 2, 반환[1,2,3,4,5,6,7,8,9,10,11,...,99]. 주의 다음과 같...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.