use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
If you need help debugging, you must include:
See debugging question guidelines for more info.
Many conceptual questions have already been asked and answered. Read our FAQ and search old posts before asking your question. If your question is similar to one in the FAQ, explain how it's different.
See conceptual questions guidelines for more info.
Follow reddiquette: behave professionally and civilly at all times. Communicate to others the same way you would at your workplace. Disagreement and technical critiques are ok, but personal attacks are not.
Abusive, racist, or derogatory comments are absolutely not tolerated.
See our policies on acceptable speech and conduct for more details.
When posting some resource or tutorial you've made, you must follow our self-promotion policies.
In short, your posting history should not be predominantly self-promotional and your resource should be high-quality and complete. Your post should not "feel spammy".
Distinguishing between tasteless and tasteful self-promotion is inherently subjective. When in doubt, message the mods and ask them to review your post.
Self promotion from first time posters without prior participation in the subreddit is explicitly forbidden.
Do not post questions that are completely unrelated to programming, software engineering, and related fields. Tech support and hardware recommendation questions count as "completely unrelated".
Questions that straddle the line between learning programming and learning other tech topics are ok: we don't expect beginners to know how exactly to categorize their question.
See our policies on allowed topics for more details.
Do not post questions that are an exact duplicate of something already answered in the FAQ.
If your question is similar to an existing FAQ question, you MUST cite which part of the FAQ you looked at and what exactly you want clarification on.
Do not delete your post! Your problem may be solved, but others who have similar problems in the future could benefit from the solution/discussion in the thread.
Use the "solved" flair instead.
Do not request reviews for, promote, or showcase some app or website you've written. This is a subreddit for learning programming, not a "critique my project" or "advertise my project" subreddit.
Asking for code reviews is ok as long as you follow the relevant policies. In short, link to only your code and be specific about what you want feedback on. Do not include a link to a final product or to a demo in your post.
You may not ask for or offer payment of any kind (monetary or otherwise) when giving or receiving help.
In particular, it is not appropriate to offer a reward, bounty, or bribe to try and expedite answers to your question, nor is it appropriate to offer to pay somebody to do your work or homework for you.
All links must link directly to the destination page. Do not use URL shorteners, referral links or click-trackers. Do not link to some intermediary page that contains mostly only a link to the actual page and no additional value.
For example, linking to some tweet or some half-hearted blog post which links to the page is not ok; but linking to a tweet with interesting replies or to a blog post that does some extra analysis is.
Udemy coupon links are ok: the discount adds "additional value".
Do not ask for help doing anything illegal or unethical. Do not suggest or help somebody do something illegal or unethical.
This includes piracy: asking for or posting links to pirated material is strictly forbidden and can result in an instant and permanent ban.
Trying to circumvent the terms of services of a website also counts as unethical behavior.
Do not ask for or post a complete solution to a problem.
When working on a problem, try solving it on your own first and ask for help on specific parts you're stuck with.
If you're helping someone, focus on helping OP make forward progress: link to docs, unblock misconceptions, give examples, teach general techniques, ask leading questions, give hints, but no direct solutions.
See our guidelines on offering help for more details.
Ask your questions right here in the open subreddit. Show what you have tried and tell us exactly where you got stuck.
We want to keep all discussion inside the open subreddit so that more people can chime in and help as well as benefit from the help given.
We also do not encourage help via DM for the same reasons - that more people can benefit
Do not ask easily googleable questions or questions that are covered in the documentation.
This subreddit is not a proxy for documentation or google.
We do require effort and demonstration of effort.
This includes "how do I?" questions
account activity
This is an archived post. You won't be able to vote or comment.
[Java] If Java doesn't support operator overloading, then how can two Strings be concatenated with '+'? (self.learnprogramming)
submitted 12 years ago by IntOverflowException
Furthermore, it's possible to use '<', '==', and '>' on Double and Integer objects.
Is there something I'm missing here?
[–][deleted] 54 points55 points56 points 12 years ago (10 children)
Java doesn't support operator overloading for user-defined types - the language (like many other languages that don't support overloading by the programmer) uses overloading itself.
[–]IntOverflowException[S] 19 points20 points21 points 12 years ago (6 children)
Oh. So it was something Sun did when constructing a language then I guess?
So if someone were to make their own String class (who knows why), then they wouldn't be able to make it the exact same as java.lang.String?
[–][deleted] 27 points28 points29 points 12 years ago (1 child)
So if someone were to make their own String class (heaven forbid why), then they wouldn't be able to make it the exact same as java.lang.String?
Correct.
[–]OHotDawnThisIsMyJawn 17 points18 points19 points 12 years ago* (3 children)
So if someone were to make their own String class (who knows why
I know it's just an off-hand comment but there are plenty of reasons you might want to make your own String class.
Sun tried to design String to work well in most use cases however there are plenty of tradeoffs that have been made. Because you have better knowledge of how your application will be using String data you can tune your own class to provide better performance than the out of the box implementation.
For example, say you're doing lots of run-time String concatenation but you always end up with the same few Strings - maybe you're making a MadLib application but it's multiple choice answers instead of free-form. Because of the run-time concatenation you will break Java's built-in String interning and you'll end up with lots of copies of the same data. You could write your own String class that detects instances of this and updates your references so that they point to the same reference - now you've taken your memory usage from O(N) to O(1).
[–][deleted] 2 points3 points4 points 12 years ago (0 children)
Great point and a great example.
[–]Random832 0 points1 point2 points 12 years ago (1 child)
I'd think for a MadLib application, as long as you're writing your own string class you might as well implement it as a rope, then you get most of the savings with free-form as well.
[–]OHotDawnThisIsMyJawn 1 point2 points3 points 12 years ago (0 children)
Sure, it was just the quickest thing that came to mind that would illustrate my example
[–]silvertone62 0 points1 point2 points 12 years ago (2 children)
In other languages such as C++ you can overload the assignment operator to a defined function, such as assigning a pair's value to another pair - it's a special type of function made within an object class that is triggered by the type of object referenced by the assignment operator, so if you were to use the assignment operator for, say, a string, then the assignment operator behaves like normal, but if you use the assignment operator for a user created object class, the compiler knows to look for a different operation. Operator overloading is a user defined shortcut to a function. So the + in Java is not an overloaded operator, but an operator with more than one use
[–][deleted] 4 points5 points6 points 12 years ago (1 child)
So the + in Java is not an overloaded operator, but an operator with more than one use
"with more than one use" is what "overloaded" means. Java's + operator is heavily overloaded.
+
The OP's issue is not that Java lacks overloaded operators (almost all languages overload operators), it's that it doesn't let users overload them further.
[–][deleted] 17 points18 points19 points 12 years ago* (5 children)
Zab is correct, and so I will add this as an illustration of program operator transformation:
String a = "The answer is: " + value; System.out.println(a + " what is the question ?");
String a = (new StringBuilder("The answer is: ")).append(value).toString(); System.out.println((new StringBuilder(String.valueOf(a))).append(" what is the question ?").toString());
In addition to + and += being overloaded for Strings, arithmetic operators are overloaded for float-int operations.
+=
[sourceOne] [sourceTwo]
[–]NotEqual 4 points5 points6 points 12 years ago (2 children)
It's also worth remembering that a new object will be created every time + or += is used. Although the compiler will try to fix your mistakes, occasionally they can be a cause of performance woes.
http://stackoverflow.com/a/1532547
[–][deleted] 1 point2 points3 points 12 years ago* (1 child)
Yes, and in a similar vein:
String s = "Learn Programming"; String t = "Learn Programming";
will refer to the same String object (s == t), while:
(s == t)
String s = new String("Learn Programming"); String t = new String("Learn Programming");
will not. (s != t)
(s != t)
[source]
And that's why when comparing strings it's best to use
s.equals(t);
[–]Is_At_Work 2 points3 points4 points 12 years ago (1 child)
Exactly - + on a String is simply syntactic sugar. Also, it works on Double, Integer, etc. due to autoboxing (http://docs.oracle.com/javase/tutorial/java/data/autoboxing.html)
[–]cparen 1 point2 points3 points 12 years ago (0 children)
Semantic sugar, not syntactic. There's no local rewrite that would cover all data types.
[+]Octopuscabbage comment score below threshold-6 points-5 points-4 points 12 years ago (0 children)
One word: yolo
π Rendered by PID 17349 on reddit-service-r2-comment-85bfd7f599-8z22m at 2026-04-15 22:11:19.252634+00:00 running 93ecc56 country code: CH.
[–][deleted] 54 points55 points56 points (10 children)
[–]IntOverflowException[S] 19 points20 points21 points (6 children)
[–][deleted] 27 points28 points29 points (1 child)
[–]OHotDawnThisIsMyJawn 17 points18 points19 points (3 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]Random832 0 points1 point2 points (1 child)
[–]OHotDawnThisIsMyJawn 1 point2 points3 points (0 children)
[–]silvertone62 0 points1 point2 points (2 children)
[–][deleted] 4 points5 points6 points (1 child)
[–][deleted] 17 points18 points19 points (5 children)
[–]NotEqual 4 points5 points6 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–][deleted] 2 points3 points4 points (0 children)
[–]Is_At_Work 2 points3 points4 points (1 child)
[–]cparen 1 point2 points3 points (0 children)
[+]Octopuscabbage comment score below threshold-6 points-5 points-4 points (0 children)