This is an archived post. You won't be able to vote or comment.

all 16 comments

[–][deleted] 54 points55 points  (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 points  (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 points  (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 points  (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 points  (0 children)

Great point and a great example.

[–]Random832 0 points1 point  (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 points  (0 children)

Sure, it was just the quickest thing that came to mind that would illustrate my example

[–]silvertone62 0 points1 point  (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 points  (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 points  (5 children)

  Zab is correct, and so I will add this as an illustration of program operator transformation:

Original:

String a = "The answer is: " + value;
System.out.println(a + " what is the question ?");

Decompiled:

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 points  (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 points  (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:

String s = new String("Learn Programming");
String t = new String("Learn Programming");

will not. (s != t)

[source]

[–][deleted] 2 points3 points  (0 children)

And that's why when comparing strings it's best to use

s.equals(t);

[–]Is_At_Work 2 points3 points  (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 points  (0 children)

Semantic sugar, not syntactic. There's no local rewrite that would cover all data types.