A. Introduction
We have learned what will happen if we mix int, float and double: the "lower" types such as int or float will be promoted to "higher" types such as double. We also learned we can add or subtract char types just like int. So what will happen if mix strings with characters and numbers?
B. Case by case, you will see:
aString + anotherString = aNewString - concatenation. e.g. "ABC"+"abc" = "ABCabc";
aString-anotherString - does not compile, "-" is not defined for strings
aString + aCharacter = aNewString - concatenation. e.g. "ABC"+'a'="ABCa"
aString+aNumber=aNewString - concatenation. e.g. ""+12 = "12"
aString - aNumber - does not compile
aString -aCharacter - does not compile
If you mix more than one type, please keep in mind the order of operations:
System.out.println(10+5+4+""); // prints 19 as a string
System.out.println(""+10+5+4); // prints 1054 as a string
System.out.println(""+(10+5)+4); // prints 154 as a string
System.out.println(""+(10+5)+'a'); // prints 15a as a string
System.out.println((10+5)+""+'a'); // prints 15a as a string
System.out.println((10+5)+'a'+"bc"); // prints 112bc as a string
System.out.println((10+5)+'a'); // prints 112
C. Try:
This Kahoot! game for strings