• Post Reply Bookmark Topic Watch Topic
  • New Topic

Overloaded Operator?  RSS feed

 
Vasiq Molvizadah
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • 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 ???
 
Vijitha Kumara
Bartender
Posts: 4002
42
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Vijitha Kumara wrote:That is you can use "+" operator with String concatenation, arithmetic addition etc..


Thanks for the explanation....
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • 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 ... ...
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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 ... ...
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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.
 
Sha Jar
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!