"And now it's time for a small but interesting topic: conversions to the String type."

"In Java, any data type can be converted to a String."

"That sounds cool."

"It's better than cool. Almost every type can be implicitly converted to a String. This is easy to see when we add two variables, where one is a String and the other is something else. The non-String variable will be converted to a String."

"Check out a couple of examples:"

Command What really happens
int x = 5;
String text = "X=" + x;
int x = 5;
String s = "X=" + Integer.toString(x);
Cat cat = new Cat("Oscar");
String text = "My cat is " + cat;
Cat cat = new Cat("Oscar");
String text = "My cat is" + cat.toString();
Object o = null;
String text = "Object is " + o;
Object o = null;
String text = "Object is " + "null";
String text = 5 + '\u0000' + "Log";
int i2 = 5 + (int) '\u0000';
String text = Integer.toString(i2) + "Log";
String text = "Object is " + (float) 2 / 3;
float f2 = ((float) 2) / 3;
String text = "Object is " + Float.toString(f2);

Conclusion: If we add a String and 'any other type', the second type will be converted to a String.

"Pay attention to line four in the table. All operations are executed from left to right. That's why adding 5 + '\u0000'" is the same as adding integers."

"So, if I write something like String s = 1+2+3+4+5+"m", I'll get s = "15m" ?"

"Yeah. The numbers will first be added, and then the sum will be converted to a string."