File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

'+' operator overloading in java

 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
+ operator is overloaded for strings in java.But,i am not clear where it is defined and how the operator overloading happens internally.Please tell me.
 
Ajay Singh
Ranch Hand
Posts: 182
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its hadden from the programmer and compiler does appending of strings for you
 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But,How does it happen internally in java,that i want to know.
 
Joe Ess
Bartender
Posts: 9214
9
Linux Mac OS X Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String concatenation is declared in the Java Specification. The compiler does the work of converting '+' into a StringBuffer creation and a series of append() calls.
 
Henry Wong
author
Marshal
Pie
Posts: 20828
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Raj Kumar Bindal:
But,How does it happen internally in java,that i want to know.


Java does not support Operator Overloading. The "+" operator on strings is specifically handled by the compiler.

As for what happens, the compiler will instantiate a StringBuffer or StringBuilder (for Java 5 and later) object, and call the append() methods with the string operands, followed by a toString() for the result.

[EDIT: Beaten to the answer again. These forums are fast... ]

Henry
[ January 12, 2007: Message edited by: Henry Wong ]
 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But,How does it happen internally in java,that i want to know.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13045
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But,How does it happen internally in java,that i want to know.

What part of what Joe and Henry said about the Java compiler treating + for Strings don't you understand?

Bill
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no Java syntax for overloading the operator. The compiler just "knows" that the operator is overloaded for the String class.

With other words, the operator overloading isn't implemented in the source code for the String class, but hardcoded into the compiler.
 
Enrico Savazzi
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Raj Kumar Bindal:
+ operator is overloaded for strings in java.But,i am not clear where it is defined and how the operator overloading happens internally.Please tell me.


If you mean what rule is applied by the compiler to decide whether to use arithmetic addition or string concatenation, it is like this:
If one or both operands are a string, + means string concatenation, else + means addition.

So, if you write "Hello" + 1 + 2, this evaluates to "Hello12" because the operators are evaluated left to right. If you change the order of evaluation with parentheses and write "Hello" + (1 + 2), the rightmost + is evaluated first, and interpreted as an addition because both operands are int literals, so the expression evaluates to "Hello3".
[ January 14, 2007: Message edited by: Enrico Savazzi ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic