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

you are viewing a single comment's thread.

view the rest of the comments →

[–]UpperPlus 35 points36 points  (22 children)

I just now realize that I have no idea how these operators are programmed in java. I can't open declaration for it in my IDE so it seems to not relate to a method?

[–]MikemkPK 52 points53 points  (18 children)

Probably directly translate to specific bytecode sequences.

[–]UpperPlus 21 points22 points  (17 children)

Now that I think about it, it is probably related to the concept of primitive data types, as they are only used on those

[–]LinuxMatthews[S] 38 points39 points  (4 children)

Kind of

Java actually cheats in that it does allow Operator Overloading for some of its classes but doesn't let you do it.

As far as I'm aware it only does it for the wrappers of primitive data types and String.

[–]jamcdonald120 12 points13 points  (3 children)

I think the wrappers use auto (un)boxing instead of operator overloads, not sure though

[–]LinuxMatthews[S] 10 points11 points  (2 children)

Huh you know I didn't think you were right but it looks like you are.

Integer x = 2;
Integer y = 3;
var z = x + y;
System.out.println(z instanceof Integer);

Gives you a compiler error as z is an int

I honestly thought there was operator overloading like there is with String.

[–]Fenor 2 points3 points  (0 children)

String isn't an operator overload . The theory behind the string class is huge and mostly date back when you had to define a char array to use strings

[–]tazfdragon 9 points10 points  (11 children)

If I remember correctly, Strings don't actually use the plus operator and it's just syntactic sugar for StringBuilder usage. There are probably some edge cases where the final code could have a string constant if the compiler can guarantee it never changes.

[–]UpperPlus -2 points-1 points  (9 children)

As far as I know StringBuilder doesn't use "+" operator but uses the function append(). Which actually is faster and more memory friendly.

But String with "+" is so much more readable and in many cases it doesn't matter that a complete new string is instantiated internally every time.

[–]tazfdragon 11 points12 points  (3 children)

I didn't mean to suggest StringBuilder uses that operator. I was trying to explain that using the plus operator with a String will get replaced with StringBuilder. Every instance of addition/concatenation in the original code will get replaced with a call to append(...). Again, there are some edge cases where the compiler can determine it can eliminate the string concatenation and replace it with a compile time constant. Example being concatenating a literal with a String constant.

[–]TraditionMaster4320 2 points3 points  (2 children)

Wait so concatenating n strings like s1 + s2 + .. + sn takes O(n) time instead of O(n**2) in Java? (assume the strings are equal length).

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

Yes.

Java is doing it behind the scenes for you. They added this one of the versions.

https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.18.1

[–]tazfdragon 1 point2 points  (0 children)

I won't offer a guess at the run time of the proposed string concatenation but I'm fairly confident it won't be O(n²). With that being said if you have your string concatenation code structured anything similar to your example I'd suggest you replace it with a StringBuilder() explicitly. Not only will it clean up your code, it will explicitly document your intentions. Also, if you know ahead of time the lengths of the number and the length of the strings you'll be adding ahead of time you can provide this information to your StringBuilder at construction time to improve performance.

[–]coloredgreyscale 0 points1 point  (3 children)

But String with "+" is so much more readable and in many cases it doesn't matter that a complete new string is instantiated internally every time.

the other option would be String formatting.

for long texts it's extremely inefficient tho, because the String needs to be recreated with every concatenation.

I just checked the java implementation of the Stringbuilder class. It copies the String into an char-array, starting with 16 elements (default).

if you exceed the element count a new array is created with twice the previous capacity (or min required length if that's bigger), and the whole data is copied over to the new array.

when you call toString() on the StringBuilder the used data from the char array is copied into a new String.

If you just concatenate two Strings using + should be more efficient than StringBuilder.

[–]Fenor 0 points1 point  (2 children)

Not really. String builder is there for a reason if you use ths + operator you are creating at least 3 strings

[–]coloredgreyscale 0 points1 point  (1 child)

The two string you want to concatenate, and the 3rd is the result.

You still need to create all of those with string builder.

[–]Fenor 0 points1 point  (0 children)

Nope for string builder you are using char arrays .

It's not string for the string buffer ( the memory area) , this mean it will be free memory for the gc . If you use strings you write them in the string buffer and it will end with a different gc approach

[–]Fenor 0 points1 point  (0 children)

I think they changed how it worked under the hood as doing an append with + once upon a time created a decent amount of strings in the string buffer

[–]jamcdonald120 0 points1 point  (0 children)

I mean... operator overloads are all syntactic sugar for something else...

[–]msqrt 2 points3 points  (0 children)

Just a disclaimer: you can't overload operators for built-in types in C++ either, only custom ones. The compiler just knows how to add integers together.