"문자열을 병합하는 방법을 알려 드리고 싶습니다. 문자열을 병합하거나 연결하는 과정은 종종 '연결'이라는 짧은 단어를 사용하여 언급됩니다. 고양이를 좋아하는 사람들은 con-Cat-en-Nation을 쉽게 기억할 것입니다. I 농담이야 ."

"문자열을 병합하는 규칙은 간단합니다. 문자열과 다른 것을 '추가'(+)하면 '다른 것'이 toString () 메서드를 통해 암시적으로 문자열로 변환됩니다. "

"방금 나랑 얘기하고 있었어?"

"알겠습니다. 쉽게 설명하겠습니다. 문자열, 숫자 및 고양이를 추가하면 숫자와 고양이가 모두 문자열로 변환됩니다. 다음은 몇 가지 예입니다."

암호 동등한 코드
Cat cat = new Cat();
String text = "The cat is " + cat;
Cat cat = new Cat();
String s = cat.toString();
String text = "The cat is " + s;
int a = 5;
String text = "a is " + a;
int a = 5;
String s = Integer.toString(a);
String text = "a is " + s;
int a = 5;
String text = a + "a is ";
int a = 5;
String s = Integer.toString(a);
String text = s + "a is ";
Cat cat = new Cat();
int a = 5;
String text = "The cat is " + cat + a;
Cat cat = new Cat();
String s1 = cat.toString();
String s2 = Integer.toString(a);
String text = "The cat is " + s1 + s2;
Cat cat = new Cat();
int a = 5;
String text = a + "The cat is " + cat + a;
Cat cat = new Cat();
String s1 = cat.toString();
String s2 = Integer.toString(a);
String s3 = Integer.toString(a);
String text = s3 + "The cat is " + s1 + s2;
Cat cat = new Cat();
int a = 5;
String text = cat + a + "The cat is " + cat + a;
프로그램이 컴파일되지 않습니다!
더하기 연산은 왼쪽에서 오른쪽으로 실행되므로 다음과 같은 결과를 얻습니다. 숫자에 고양이를 추가하면 자동 문자열 변환이 없습니다.
String text = (((cat + a) + "The cat is ") + cat) + a;
// But you can do this:
Cat cat = new Cat();
int a = 5;
String text = cat + (a + "The cat is ") + cat + a;

// This is the same as:
Cat cat = new Cat();
int a = 5;
String text = ((cat + (a + "The cat is ")) + cat)+a;
Cat cat = new Cat();
String s1 = cat.toString();
String s2 = cat.toString();
String s3 = Integer.toString(a);
String s4 = Integer.toString(a);
String text = s1 + s3 + "The cat is " + s2 + s4;

"디에고에게서 몇 가지 일을 할 때가 되었습니다."