그럼 지금까지 설명한 내용을 코드에서 다시 한번 실행해볼까요?
package javaStudy;
public class StringMethodExam {
public static void main(String[] args) {
String str = "hello";
System.out.println(str.length());
System.out.println(str.concat(" world"));
System.out.println(str);
str = str.concat(" world");
System.out.println(str);
}
}
str이 hello world를 가리키도록 코드를 작성했습니다. concat 메서드를 사용해 문자열을 붙여줘야 str이 hello world를 가리킵니다.
실행결과
5
hello world
hello
hello world