• 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

is there any way to implement operator overload ?

 
Ranch Hand
Posts: 620
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, Java operators cannot be overloaded by programmers.
 
ben josh
Ranch Hand
Posts: 620
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
 
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic