• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Overloaded Operator?

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've been reading Head First Java book by Kathy Sierra and Berry Bates..and there's a statement which i didn't understand properly..

" + operator is overloaded in Java (the only overloaded operator)"...

What is overloaded operator ???
 
Bartender
Posts: 4109
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is you can use "+" operator with String concatenation, arithmetic addition etc..
 
Vasiq Molvizadah
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vijitha Kumara wrote:That is you can use "+" operator with String concatenation, arithmetic addition etc..



Thanks for the explanation....
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A minor clarification... Overloading of "+" in Java applies to
the String class only, as below. All arithmetic operations are
the normal non-overloaded use of the symbol.
Jim ... ...
 
Marshal
Posts: 74342
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . but it doesn't work on BigDecimal and BigInteger at all. Many people think another overloading for BigDecimal and BigInteger would be useful.
 
Sheriff
Posts: 22504
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which is why it is (or was?) under consideration for Java 7.
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it doesn't work for the "Big..." classes, only String.
(Integer + Long) works because of auto-boxing combined
with type promotion, not because '+' is overloaded.
Jim ... ...
 
lowercase baba
Posts: 13013
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In C++, you could overload any operator you wanted. You could define the '+' symbol to add two widgets together, and then write whatever made sense for that class.

It got a little crazy.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:In C++, you could overload any operator you wanted. You could define the '+' symbol to add two widgets together, and then write whatever made sense for that class.

It got a little crazy.



In Java it's controversial. One side argues that general operator overloading should be introduced. Then the programmer would decide what to overload, like in C++ and C#. The other side argues it's better to have fixed overloading and introduce new overloadings on a need to have basis. It seems the latter is in majority.
reply
    Bookmark Topic Watch Topic
  • New Topic