Quantcast
Channel: Reza's Weblog
Viewing all articles
Browse latest Browse all 50

String Concatenation with +

$
0
0

The + operator performs string concatenation if and only if at least one of its operands is of type String; otherwise, it performs addition. In this case 'a'+ 'b' prints an int number not the string ab.

To force this into a String concatenation we can append an empty string; use a string buffer; or if you are using release 5+, use the printf() facilities.

The + operator first performs string conversion on both of its operands and then concatenates the resulting strings. String conversion for object references, which include arrays, is done using their toString methods. So the output for the following is NOT Hi21. You can try!

        char[] numbers = { '2','1'};
        System.out.println(" Hi " + numbers);

Viewing all articles
Browse latest Browse all 50

Trending Articles