• Post Reply Bookmark Topic Watch Topic
  • New Topic

is there any way to implement operator overload ?  RSS feed

 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all
from what im reading there is no operator overload in java
is there any trick or tip to mimic this in java?
thanks
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, Java operators cannot be overloaded by programmers.
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok .. i see
this leads me to other question is there any way to define in java some kind global object that can hold any primitive data type
(int,long,string...)
so if i will define :
myGlobalType foo = "string1";
myGlobalType foo1 = "string2";
and if i will do :
foo+foo1 =string1string2"
and if i will do
myGlobalType foo = 2;
myGlobalType foo1 = 3;
and if i will do :
foo+foo1 =5

can it be implement this somehow in java
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think so. The + operator is overloaded to add values and concatenate Strings. If object references are used in this context, the object's toString method is called, and the returned String is concatenated, so I don't see how you could "fool" Java into adding ref1 + ref2.

(By the way, a Java String is an Object -- not a primitive.)
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What he wants isn't going to work in any language.
Languages that do support operator overloading will need specific mappings for each set of datatypes an operator will operate on, he seems to think that there is some magical generic solution that will make any operator work with any set of datatypes almost automagically.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!