LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-20-2006, 10:53 AM   #1
arunvk
Member
 
Registered: Nov 2005
Location: India
Distribution: Fedora 29
Posts: 197

Rep: Reputation: 30
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.
 
Old 03-20-2006, 11:11 AM   #2
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 65
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).
 
Old 03-20-2006, 03:42 PM   #3
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
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.)
 
Old 03-20-2006, 07:27 PM   #4
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
Quote:
Don't try to use (...) logic in Java
'couldn't've said it better myself
 
Old 03-22-2006, 11:03 AM   #5
arunvk
Member
 
Registered: Nov 2005
Location: India
Distribution: Fedora 29
Posts: 197

Original Poster
Rep: Reputation: 30
thanks all. i understood it now.
 
Old 03-22-2006, 12:11 PM   #6
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 65
Quote:
Originally Posted by jonaskoelker
'couldn't've said it better myself
ghehe . I hope I didn't bash Java that much. It's still my favorite language though
 
Old 03-23-2006, 07:31 AM   #7
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems with overloading operator+ in C++ niteshadw Programming 13 07-20-2005 07:45 PM
Operator Overloading problem ashwinipahuja Programming 1 06-23-2004 05:59 AM
c++ [] operator overloading exodist Programming 12 04-17-2004 03:06 PM
c++ overloading input operator true_atlantis Programming 4 02-25-2004 07:24 PM
Operator Overloading - Java? Tesl Programming 4 02-13-2004 12:54 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration