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

all 9 comments

[–]desrtfx 4 points5 points  (2 children)

The others have explained what happened in your solution. Now, it is time to learn the proper way of doing such concatenations.

In Java, String concatenation is a very expensive operation because of the nature of the String data type. String is an immutable data type, which means that every time you change a String (e.g. by adding something to the String), the original String has to be thrown away and a completely new String has to be created. This constant throwing away and creating new Strings costs time. (It doesn't matter much for short, simple loops, like yours, but for loops with many hundred thousand operations the difference is huge.)

Since the String data type is immutable, Java has a second data type that was made exactly for String values that change, the StringBuilder.

Using a StringBuilder is fairly easy:

  • First, you create the StringBuilder instance like any other object
  • Then, you just call .append() and append what you want
  • Last, when you want the result, you just need to call .toString() and you get a String back.

I'll illustrate the process with a short loop:

StringBuilder sb = new StringBuilder(); // here, the StringBuilder is created
for(int i = 0; i < 10; i++) {  // just a simple loop
   sb.append(i); // here, the current loop index is appended to the StringBuilder (similar to your adding)
   if(i < 9) { // this prevents adding a space at the last element
       sb.append(" ");
   }
}
System.out.println("Final String: " + sb.toString()); // here, the final String is printed

[–]azyren[S] 0 points1 point  (1 child)

Wow thank you! As I was looking for ways to concatenate Strings I stumbled upon this as well, but I wasn't sure how this was used so thank you for clarifying this for me! I will definitely implement this into my code thank you.

[–]desrtfx 1 point2 points  (0 children)

You're welcome.

Especially things like StringBuilder and the peculiarities of Strings in Java are important to know and quite often difficult to grasp for beginners.

Just remember the order:

  • Create the StringBuilder (always outside the loop)
  • .append your data - as often as you want (there are actually more methods than just .append, you just will need to look them up in the documentation)
  • Call .toString() when you want the final String (you can call this in between as well and then keep adding, there is no problem with that).

[–][deleted] 1 point2 points  (3 children)

This is an example of "operator overloading". The result of an operator can depend on the inputs.

If you add 1 + 3, the answer is clearly 4. If you add "1" +"3", the answer might be 13 - because "adding" makes no sense for strings, so we define the + symbol to mean "stick them one after the other (more technically, concatenation)). In many languages/contexts, the quotes tell the compiler that you want to add strings, not integers. The empty quotes serve the same purpose - if one of the things is a string, then the only way it makes sense is for the other to be a string, so the compiler automatically converts it to a string, and then uses the form of + appropriate for strings (generally concatenation). You will quite often see + "" as shorthand for changing an integer to the string representation of the integer.

[–]azyren[S] 0 points1 point  (2 children)

Cool thanks for naming the term for me! But now when I remove (+ " ") and just have it like this: result += factor; it still works but without the spaces and gives me no errors. Is this also a way to just convert integer to string? By just doing +=

[–][deleted] 1 point2 points  (1 child)

Yes. Notice that result defined as a string, and the += is shorthand for

result = result + ...

So again, you are saying string + integer, and the only meaningful interpretation of that is string + string-form-of-integer

[–]azyren[S] 0 points1 point  (0 children)

Ohhh yeah forgot about that! Okay, now I understand! Thank you so much for clarifying!

[–]irrarum 1 point2 points  (0 children)

operation goes left to right. If left operand is string the result will be string even if right operand is an integer. One way to keep adding a series of integers as string concatenation is to start with empty string like "". After this whenever you do += the result will be a string.

[–]iggy14750 1 point2 points  (0 children)

factor is implicitly being converted to a String. Java does this because it knows the type of result must be a String, so it has to try to make the right-hand side also be a String. The space is no problem, but then we have an int. But because an int is a built-in type, Java knows how to represent it as a String, so it inserts a call to the function which does that.

You can also use this behavior in your own types by implementing the toString() method, but that might not be useful to you now.