• Post Reply Bookmark Topic Watch Topic
  • New Topic

operator overloading..  RSS feed

 
vignesh hariharan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • 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???
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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 ]
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • 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
  • 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///
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37462
537
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
thank you people...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!