"Jeg vil gerne fortælle dig, hvordan du flette strenge. Processen med at flette eller forbinde strenge omtales ofte ved at bruge det korte ord 'sammenkædning'. Katteelskere vil finde det nemt at huske: con-Cat-en-Nation. I jeg spøger ."

"Reglerne for at flette strenge er enkle. Hvis vi 'tilføjer' (+) en streng og noget andet, så konverteres 'noget andet' implicit til en streng via toString ()-metoden . "

"Snakket du til mig lige nu?"

"Okay, jeg vil forklare det på en lettere måde. Hvis vi tilføjer en streng, et tal og en kat, så bliver både nummeret og katten forvandlet til strenge. Her er nogle eksempler:"

Kode Tilsvarende kode
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;
Programmet vil ikke kompilere!
Tilføjelsesoperationerne udføres fra venstre mod højre, så vi får: Hvis vi tilføjer en kat til et tal, er der ingen automatisk strengkonvertering.
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;

"Tiden er inde til at udføre et par opgaver fra Diego."