The addition operator (+) allows concatenation of Strings. By using the multiplication operator (*), Strings can be concatenated several times, e.g.,
("MINI" + "MOS" + "-NT") // -> "MINIMOS-NT" ("Vienna" * 3) // -> "ViennaViennaVienna" // The * operator can be used to create long lines very easily: ("-" * 25) // -> "-------------------------"
Other data types can be connected to a String as well, whereby non-String values are converted to their String representation as shown in the example below:
3 + " times " + 5 + " equals " + (3 * 5) // -> "3 times 5 equals 15" 5.0 + " over " + 2.0 + " equals " + (5.0 / 2.0) // -> "5.0 over 2.0 equals 2.5" "i1 = " + 8 mA // -> "i1 = 0.008 A"
When using the String operators + or * the strongest type of an operand is String. Adding some numbers to a String is equivalent to concatenating their String representations.
This behavior may deliver unexpected results. If there is any doubt, expressions should be enclosed in parentheses, e.g.,
3 + " plus " + 5 + " equals " + 3 + 5 // -> "3 plus 5 equals 35" -> Wrong!! 3 + " plus " + 5 + " equals " + (3 + 5) // -> "3 plus 5 equals 8" -> Correct!!
Robert Klima 2003-02-06