The + operator on String objects

  1. Java treats Strings special and has a + operator on Strings:

        String1 + String2          // Concatenate String1 and String2
    
    is short hand for:
        String1.concat(String2)    // Concatenate String1 and String2
    

    Example: "Hello " + "World !" results in a new String "Hello World !"

  2. Furthermore... when the + operator operates on a String and another type of value, then the other type of value is converted to a String

        String + AnotherType  -->  String + String
        AnotherType + String  -->  String + String
    

  3. The "other type" can be:

    NOTE: this looks awfully lot like a widening conversion !!!


    Example:
    
        "Hello" + 123  --> "Hello" + "123" --> "Hello123"
    
        "Hello" + 1 + 23  --> ...details explained below
    		      --> ("Hello" + 1) + 23
    		      --> ("Hello" + "1") + 23
    		      --> "Hello1" + 23
    		      --> "Hello1" + "23"
    		      --> "Hello123"
    
        123 + "Hello" --> "123" + "Hello" --> "123Hello"
    
        1 + 23 + "Hello"  --> ...details explained below
    		      --> (1 + 23) + "Hello"
    		      --> 24 + "Hello"
    		      --> "24" + "Hello"
    		      --> "24Hello"
    

  4. How Java performs the "toString" conversion: