• 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..

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is the plus symbol explicitly over loaded in java??? if yes where it is overloaded.. i mean in which class the code is there???
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, "+" has several meanings in Java, as do a number of other operators. No, you can't overload operators in Java code; there's no syntax for defining operators. The Java compiler itself implements the special meaning of the "+" operator for Strings -- i.e., the compiler knows about the String class and treats it as a special case.
[ September 02, 2006: Message edited by: Ernest Friedman-Hill ]
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, Java has operators overloaded defined by the language (and not the language user). The + operator is one of them - accepting operands of varying types.

Edit: spelling
[ September 02, 2006: Message edited by: Tony Morris ]
 
vignesh hariharan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java is built on java.. then if no operator overloading syntax available.. then how come the language developers created that?? am little confused.. please explain me///
 
author & internet detective
Posts: 41878
909
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by vignesh hariharan:
then if no operator overloading syntax available.. then how come the language developers created that?? am little confused.. please explain me///


The language developers didn't create overloading. They specified a list of types that can be "added" and what happens when they are. Then they added all the primitives and the String class to that list.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by vignesh hariharan:
then if no operator overloading syntax available.. then how come the language developers created that?? am little confused.. please explain me///



They cheated. When the Java compiler hits on code like

string1 + string2

it knows to compile it to the bytecode for

new StringBuffer().append(string1).append(string2).toString();

That knowledge is hardcoded into the compiler - you won't find any code for it in the source of the String class.

So, if you wanted to have operator overloading for your own classes, you'd need to do the same - writing your own compiler.
 
vignesh hariharan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you people...
 
I AM MIGHTY! Especially when I hold this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic