String word1 = "Hello";
String word2 = "World";
String message = word1 + " " + word2 + " " + 2022 +"!";
System.out.println(message);
Output on console: Hello World 2022!
Note that I didn't add double quotes in 2022
(so it's not a String nor a variable) and even then the code ran without errors. Why?
If I had typed something like:
String message = word1 + " " + word2 + " " + welcome +"!";
I would obviously have gotten an error since welcome
is not a String nor a variable, but why doesn't this error happen when you concatenate integers (not a primitive integer value, but literally an integer number) like in the previous example that had 2022
concatenated with word1
and word2
variables?