# Use String Literals
# Description
Removes all class instantiations from String if its parameter is empty or a String. An empty parameter is replaced with the empty String and the construction of a String or String-literal removes the constructor.
# Benefits
Avoiding the constructor call has performance benefits as it reduces memory usage and improves readability.
Furthermore, these constructors are deprecated in Java 9, which is an indication that they will eventually be removed from the language altogether.
# Tags
# Code Changes
Pre
public String testNewStringOfLiteral() {
return new String("StringLiteral");
}
public String testNewStringOfLiteralWithParentheses() {
return new String(("StringLiteral"));
}
public String testNewStringOfOtherString(String s) {
return new String(s);
}
public String testNewStringOnNonStringElement(StringBuilder sb) {
return new String(sb);
}
Post
public String testNewStringOfLiteral() {
return "StringLiteral";
}
public String testNewStringOfLiteralWithParentheses() {
return "StringLiteral";
}
public String testNewStringOfOtherString(String s) {
return s;
}
public String testNewStringOnNonStringElement(StringBuilder sb) {
return new String(sb);
}
Use a Java Refactoring Tool
No license required
You can review this refactoring on your code without a license by installing jSparrow to your Eclipse IDE. Install the plug-in from Eclipse IDE: Eclipse Marketplace.
System-wide Refactoring
Do you want to automate this refactoring (and many more) to your system-wide code? The automatic application of this system-wide refactoring can be unlocked by acquiring your jSparrow license.