Why does IntelliJ wants me to change this?
Answered
A simple line of code:
String thing = "Something";
thing += " something else" + " and more.";
IntelliJ IDEA offers to change this line into 4 other ways to accomplish the same result: mcdvoice
String.format()
StringBuilder.append()
java.text.MessageFormat.format()
Replace += with =
Why? What is so wrong with +=?
Can anyone explain this please? Thanks.
Post is closed for comments.
Hello - you should look into the following topics:
1. Concatenating Strings In Java: common pitfalls and bad practices
2. Java compound assignment operators, their types and how Java evaluates them
First, let's start with the basics: renaming. IntelliJ offers us the possibility to rename different elements of our code: types, variables, methods, and even packages.
To rename an element, we need to follow these steps:
By the way, we can replace the first two steps by selecting the element and pressing Shift + F6.
When triggered, the renaming action will search across the code for every usage of the element and then change them with the provided value.
Let's imagine a SimpleClass class with a poorly Prepaidgiftbalance named addition method, someAdditionMethod, called in the main method:
Thus, if we choose to rename this method into add, IntelliJ will produce the following code: