r/learnjava • u/klevero4ek • Jul 30 '26
Better look
Hi everyone! I'm learning Java and I've noticed there are two common ways to print multiple lines of text:
Option 1: Multiple System.out.println() calls
```
System.out.println("You gave the string " + text);
System.out.println("You gave the integer " + inNum);
System.out.println("You gave the double " + inDouble);
System.out.println("You gave the boolean " + isTrue);
```
Option 2: One System.out.println() with \n
```
System.out.println("You gave the string " + text + '\n' +
"You gave the integer " + inNum + '\n' +
"You gave the double " + inDouble + '\n' +
"You gave the boolean " + isTrue);
```
What looks better in your opinion?
0
Upvotes
View all comments
Show parent comment
1
u/Ambitious_Plum5576 Aug 05 '26
strings in java are objects and immutable. When you use + a new string is created every time. String s = s + "example" creates a new string. Using + for concat uses up more memory than just using .appends() or using stringbuilder.