I presume you mean fun as in enjoyment not as in the keyword in some languages meaning function. System.out.println is not actually a function.
What you have is the behaviour of the + operator. It has two distinct behaviours, and you can read about them in the Java Language Specification (=JLS).
Applied to numbers it does addition. As in 1 + 2 = 3. Remember that if you write ' ' you are not writing the space character you are writing a number (I think 0x20 = 32 decimal) equal to its ASCII or Unicode value. Look in the JLS where it tells you a char is an (unsigned) integer. If you apply the number 2 and the ' ' char you are adding (arithmetic).
If ether of the operands is a String, however, + moves out of the realms of arithmetic into String catenation. Both its operands are Strings. So if you have 34 + "text", the 43 is converted to the String "34" and then catenated to the other String making "34text".
Pattern.length() is a number, so when added to the space (32) you get 34. Then the 34 is converted to "34" and then the two Strings are joined together. You get similar effects with this sort of code:-
Don't copy‑and‑paste this post because there are several Unicode characters which may give incorrect results.
Campbell Ritchie wrote:I presume you mean fun as in enjoyment not as in the keyword in some languages meaning function. System.out.println is not actually a function.
What you have is the behaviour of the + operator. It has two distinct behaviours, and you can read about them ...
(I shortened your reply in quote per your instructions to avoid any unicode related issues)
Thank you for your reply.
Yes, I did mean fun as in enjoyment.
I understand that operator + is overloaded: Here if lhs of the + is a string then rhs is converted to string representation and concatenated
(I was just sharing this observation on this forum so that it might help someone new to the language and programming to see how conversions are happening.
Sorry if my post gave impression that I having trouble with my code)