Possible Duplicate:
Why does + work with Strings in Java?
The following statements are valid in Java.
int a=50;
String tmp="a = ";
String b=tmp+a;
b
of type String now contains a = 50
(as a String).
Although tmp
is of type String and a
is of type int
, the concatenation is made (even though Java doesn't support operator overloading).
One of the reasons why Java doesn't support operator overloading (as with other languages. In fact, I have no depth knowledge of any language).
Java does not support operator overloading. Operator overloading is sometimes a source of ambiguity in C++ program, and the Java design team felt that it causes more trouble than benefit.
More about it.
How is this statement String b=tmp+a;
evaluated? There must be some equivalent concept of Operator Overloading internally.
Just one question : Can we see literally how it's implemented or we should just believe "It's just a feature of the language"?
I have heard that the Java compiler uses StringBuilder/StringBuffer
(with the append()
method) to achieve this but I'm not sure about that.