LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   java...operator overloading question (https://www.linuxquestions.org/questions/programming-9/java-operator-overloading-question-426621/)

arunvk 03-20-2006 10:53 AM

java...operator overloading question
 
Hello,

in java operator overloading is not supported but the following code works:-

s3=s1+s2;

where s1,s2,s3 are strings.

isnt this also a form of operator overloading. i mean s2 and s3 are objects of class string and hence not basic datatypes. so how is the "+" operator doing its function here.

thanks.

Mega Man X 03-20-2006 11:11 AM

The + in this case works as a String concatenator. Don't try to use other languages logic in Java because it won't work. I gave up on that a long ago :). Even the API is a bit inconsistent until 1.4.x, like using constants in lowercase (eg: Color.blue). They've fixed a bunch of things in 1.5.0 though (eg: Color.BLUE).

spooon 03-20-2006 03:42 PM

Quote:

Originally Posted by arunvk
Hello,

in java operator overloading is not supported but the following code works:-

s3=s1+s2;

where s1,s2,s3 are strings.

isnt this also a form of operator overloading. i mean s2 and s3 are objects of class string and hence not basic datatypes. so how is the "+" operator doing its function here.

thanks.

Yes, Java does not have operator overloading. The + for Strings is a special shortcut syntax that is specially defined in the Java specification. (It actually just gets converted to a series of method calls by the parser.)

jonaskoelker 03-20-2006 07:27 PM

Quote:

Don't try to use (...) logic in Java
'couldn't've said it better myself :D

arunvk 03-22-2006 11:03 AM

thanks all. i understood it now.

Mega Man X 03-22-2006 12:11 PM

Quote:

Originally Posted by jonaskoelker
'couldn't've said it better myself :D

ghehe :). I hope I didn't bash Java that much. It's still my favorite language though ;)

jonaskoelker 03-23-2006 07:31 AM

Quote:

I hope I didn't bash Java that much.
No, I deliberately snipped something to completely alter the meaning of what you said, in order to bash Java.

I'll happily do it again if you feed me ;)


All times are GMT -5. The time now is 11:10 PM.