"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 |
---|---|
|
|
|
|
|
|
|
|
|
|
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."
GO TO FULL VERSION