"Size dizileri nasıl birleştireceğinizi anlatmak istiyorum. Dizileri birleştirme veya birleştirme işlemine genellikle 'concatenation' kısa sözcüğü kullanılarak başvurulur. Kedi severlerin hatırlaması kolay olacaktır: con-Cat-en-Nation. I şaka yapıyorum ."

"Dizeleri birleştirmenin kuralları basittir. Eğer bir dize ve başka bir şey 'eklersek' (+), o zaman 'başka bir şey', toString () yöntemi aracılığıyla dolaylı olarak bir dizeye dönüştürülür. "

"Az önce benimle mi konuşuyordun?"

"Tamam daha kolay anlatayım. Bir dizi, bir sayı ve bir kedi eklersek hem sayı hem de kedi diziye dönüşür. İşte bazı örnekler:"

kod eşdeğer kod
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;
Program derlenmeyecek!
Toplama işlemleri soldan sağa yürütülür, böylece şunu elde ederiz: Bir sayıya bir kedi eklersek, otomatik dizi dönüştürme olmaz.
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;

"Diego'dan birkaç görev yapma zamanı geldi."