File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes '+' operator overloading in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark " Watch " New topic
Author

'+' operator overloading in java

Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
+ 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

Joined: Dec 13, 2006
Posts: 182
Its hadden from the programmer and compiler does appending of strings for you
Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
But,How does it happen internally in java,that i want to know.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8836
    
    7

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.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18529
    
  40

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 ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
But,How does it happen internally in java,that i want to know.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
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

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Enrico Savazzi
Greenhorn

Joined: Jan 01, 2007
Posts: 7
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 ]

--ES
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: '+' operator overloading in java
 
Similar Threads
interview question
Doubt.......null?
Advice on BigDecimal vs. double
operator overloading
Re: Operator Overloading