• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

'+' operator overloading in java

 
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But,How does it happen internally in java,that i want to know.
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But,How does it happen internally in java,that i want to know.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
reply
    Bookmark Topic Watch Topic
  • New Topic