Java: Best practices on string concatenation and variable substituittion in strings [closed]
up vote
1
down vote
favorite
There are way too many ways to concatenate strings and add variable values in Java. How should I select one (pros, cons, best use cases, etc).
- MessageFormat.format
- String.format
- "string a" + "string b"
- StringBuilder
- StringBuffer
- String.concat
- Streams
- String.join()
- Apache Commons’ StringUtils
- Google Guava’s Joiner
- ...
java string
closed as too broad by Carlos Heuberger, Andy Turner, ᴇʟᴇvᴀтᴇ, Nikolas, Daniel Pryden Nov 8 at 20:12
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
up vote
1
down vote
favorite
There are way too many ways to concatenate strings and add variable values in Java. How should I select one (pros, cons, best use cases, etc).
- MessageFormat.format
- String.format
- "string a" + "string b"
- StringBuilder
- StringBuffer
- String.concat
- Streams
- String.join()
- Apache Commons’ StringUtils
- Google Guava’s Joiner
- ...
java string
closed as too broad by Carlos Heuberger, Andy Turner, ᴇʟᴇvᴀтᴇ, Nikolas, Daniel Pryden Nov 8 at 20:12
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
2
This question seems impossibly broad. The best one to use depends on the use case. What use cases do you have in mind?
– Daniel Pryden
Nov 8 at 18:47
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
There are way too many ways to concatenate strings and add variable values in Java. How should I select one (pros, cons, best use cases, etc).
- MessageFormat.format
- String.format
- "string a" + "string b"
- StringBuilder
- StringBuffer
- String.concat
- Streams
- String.join()
- Apache Commons’ StringUtils
- Google Guava’s Joiner
- ...
java string
There are way too many ways to concatenate strings and add variable values in Java. How should I select one (pros, cons, best use cases, etc).
- MessageFormat.format
- String.format
- "string a" + "string b"
- StringBuilder
- StringBuffer
- String.concat
- Streams
- String.join()
- Apache Commons’ StringUtils
- Google Guava’s Joiner
- ...
java string
java string
edited Nov 8 at 18:44
senjin.hajrulahovic
5421417
5421417
asked Nov 8 at 18:39
Fernando Santos
92
92
closed as too broad by Carlos Heuberger, Andy Turner, ᴇʟᴇvᴀтᴇ, Nikolas, Daniel Pryden Nov 8 at 20:12
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as too broad by Carlos Heuberger, Andy Turner, ᴇʟᴇvᴀтᴇ, Nikolas, Daniel Pryden Nov 8 at 20:12
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
2
This question seems impossibly broad. The best one to use depends on the use case. What use cases do you have in mind?
– Daniel Pryden
Nov 8 at 18:47
add a comment |
2
This question seems impossibly broad. The best one to use depends on the use case. What use cases do you have in mind?
– Daniel Pryden
Nov 8 at 18:47
2
2
This question seems impossibly broad. The best one to use depends on the use case. What use cases do you have in mind?
– Daniel Pryden
Nov 8 at 18:47
This question seems impossibly broad. The best one to use depends on the use case. What use cases do you have in mind?
– Daniel Pryden
Nov 8 at 18:47
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
MessageFormat.format() - Used for dynamically created strings, where parts of the string are positioned and the arguments fill up the place.
MessageFormat.format("My name is {0}. I am {1} years old", "Vignesh", 24);
String.format() - Like position numbering in MessageFormat, it accepts the argument type specifiers.
String.format("Pi is %.2f", 3.14)
String+String - string+string produces a new string leaving the older ones in the garbage, which gets cleared later by JVM.
It internally gets converted to StringBuilder.append() and toString() methods.
hello+world=helloworld
null+hello=nullhello
String.concat() - Unlike string+string, if the object on which concat method is called is null, NullPointerException will be thrown.
String a = null, b="hello"; a.concat(b) throws NullPointerException
StringBuffer - They are mutable but they are slower as the methods inside them are synchronized. ie., thread safe
StringBuffer sb = new StringBuffer();
sb.append("hello").append("world");
sb.toString();
StringBuilder - They are mutable and faster than StringBuffer, but not thread safe
StringBuilder sb = new StringBuilder();
sb.append("hello").append("world");
sb.toString();
String.join - If the strings to be concatenated is in the form of array, its better to use String.join rather than looping through the array and appending using a StringBuilder, which String.join does it already inernally. If the array passed is null
, it throws NullPointerException
.
String a = {"hello", "world"};
String.join("", a)
StringUtils.join - If the strings to be concatenated is in the form of array, this can also be used. It internally uses StringBuilder. But just for string concatenation there is no need to include a jar. It precalcualtes the capacity
of the StringBuilder object based on the numnber of elements in the array. If the array passed is null
, it doesn't throws exception but just returns null
string.
String a = {"hello", "world"};
StringUtils.join(a, "")
add a comment |
up vote
0
down vote
A few tips:
- StringBuffer - when working with many strings
- StringBuilder - if you use String concatenation in a loop
- String.concat - only when you have to concat two string variables.
2
StringBuffer: if you use String concatenation in a loop from multiple threads (it is basically just a synchronized StringBuilder); in other words, basically never. (Or to pass to an API that was designed before StringBuilder, and was never retrofitted to accept StringBuilder).
– Andy Turner
Nov 8 at 19:06
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
MessageFormat.format() - Used for dynamically created strings, where parts of the string are positioned and the arguments fill up the place.
MessageFormat.format("My name is {0}. I am {1} years old", "Vignesh", 24);
String.format() - Like position numbering in MessageFormat, it accepts the argument type specifiers.
String.format("Pi is %.2f", 3.14)
String+String - string+string produces a new string leaving the older ones in the garbage, which gets cleared later by JVM.
It internally gets converted to StringBuilder.append() and toString() methods.
hello+world=helloworld
null+hello=nullhello
String.concat() - Unlike string+string, if the object on which concat method is called is null, NullPointerException will be thrown.
String a = null, b="hello"; a.concat(b) throws NullPointerException
StringBuffer - They are mutable but they are slower as the methods inside them are synchronized. ie., thread safe
StringBuffer sb = new StringBuffer();
sb.append("hello").append("world");
sb.toString();
StringBuilder - They are mutable and faster than StringBuffer, but not thread safe
StringBuilder sb = new StringBuilder();
sb.append("hello").append("world");
sb.toString();
String.join - If the strings to be concatenated is in the form of array, its better to use String.join rather than looping through the array and appending using a StringBuilder, which String.join does it already inernally. If the array passed is null
, it throws NullPointerException
.
String a = {"hello", "world"};
String.join("", a)
StringUtils.join - If the strings to be concatenated is in the form of array, this can also be used. It internally uses StringBuilder. But just for string concatenation there is no need to include a jar. It precalcualtes the capacity
of the StringBuilder object based on the numnber of elements in the array. If the array passed is null
, it doesn't throws exception but just returns null
string.
String a = {"hello", "world"};
StringUtils.join(a, "")
add a comment |
up vote
2
down vote
MessageFormat.format() - Used for dynamically created strings, where parts of the string are positioned and the arguments fill up the place.
MessageFormat.format("My name is {0}. I am {1} years old", "Vignesh", 24);
String.format() - Like position numbering in MessageFormat, it accepts the argument type specifiers.
String.format("Pi is %.2f", 3.14)
String+String - string+string produces a new string leaving the older ones in the garbage, which gets cleared later by JVM.
It internally gets converted to StringBuilder.append() and toString() methods.
hello+world=helloworld
null+hello=nullhello
String.concat() - Unlike string+string, if the object on which concat method is called is null, NullPointerException will be thrown.
String a = null, b="hello"; a.concat(b) throws NullPointerException
StringBuffer - They are mutable but they are slower as the methods inside them are synchronized. ie., thread safe
StringBuffer sb = new StringBuffer();
sb.append("hello").append("world");
sb.toString();
StringBuilder - They are mutable and faster than StringBuffer, but not thread safe
StringBuilder sb = new StringBuilder();
sb.append("hello").append("world");
sb.toString();
String.join - If the strings to be concatenated is in the form of array, its better to use String.join rather than looping through the array and appending using a StringBuilder, which String.join does it already inernally. If the array passed is null
, it throws NullPointerException
.
String a = {"hello", "world"};
String.join("", a)
StringUtils.join - If the strings to be concatenated is in the form of array, this can also be used. It internally uses StringBuilder. But just for string concatenation there is no need to include a jar. It precalcualtes the capacity
of the StringBuilder object based on the numnber of elements in the array. If the array passed is null
, it doesn't throws exception but just returns null
string.
String a = {"hello", "world"};
StringUtils.join(a, "")
add a comment |
up vote
2
down vote
up vote
2
down vote
MessageFormat.format() - Used for dynamically created strings, where parts of the string are positioned and the arguments fill up the place.
MessageFormat.format("My name is {0}. I am {1} years old", "Vignesh", 24);
String.format() - Like position numbering in MessageFormat, it accepts the argument type specifiers.
String.format("Pi is %.2f", 3.14)
String+String - string+string produces a new string leaving the older ones in the garbage, which gets cleared later by JVM.
It internally gets converted to StringBuilder.append() and toString() methods.
hello+world=helloworld
null+hello=nullhello
String.concat() - Unlike string+string, if the object on which concat method is called is null, NullPointerException will be thrown.
String a = null, b="hello"; a.concat(b) throws NullPointerException
StringBuffer - They are mutable but they are slower as the methods inside them are synchronized. ie., thread safe
StringBuffer sb = new StringBuffer();
sb.append("hello").append("world");
sb.toString();
StringBuilder - They are mutable and faster than StringBuffer, but not thread safe
StringBuilder sb = new StringBuilder();
sb.append("hello").append("world");
sb.toString();
String.join - If the strings to be concatenated is in the form of array, its better to use String.join rather than looping through the array and appending using a StringBuilder, which String.join does it already inernally. If the array passed is null
, it throws NullPointerException
.
String a = {"hello", "world"};
String.join("", a)
StringUtils.join - If the strings to be concatenated is in the form of array, this can also be used. It internally uses StringBuilder. But just for string concatenation there is no need to include a jar. It precalcualtes the capacity
of the StringBuilder object based on the numnber of elements in the array. If the array passed is null
, it doesn't throws exception but just returns null
string.
String a = {"hello", "world"};
StringUtils.join(a, "")
MessageFormat.format() - Used for dynamically created strings, where parts of the string are positioned and the arguments fill up the place.
MessageFormat.format("My name is {0}. I am {1} years old", "Vignesh", 24);
String.format() - Like position numbering in MessageFormat, it accepts the argument type specifiers.
String.format("Pi is %.2f", 3.14)
String+String - string+string produces a new string leaving the older ones in the garbage, which gets cleared later by JVM.
It internally gets converted to StringBuilder.append() and toString() methods.
hello+world=helloworld
null+hello=nullhello
String.concat() - Unlike string+string, if the object on which concat method is called is null, NullPointerException will be thrown.
String a = null, b="hello"; a.concat(b) throws NullPointerException
StringBuffer - They are mutable but they are slower as the methods inside them are synchronized. ie., thread safe
StringBuffer sb = new StringBuffer();
sb.append("hello").append("world");
sb.toString();
StringBuilder - They are mutable and faster than StringBuffer, but not thread safe
StringBuilder sb = new StringBuilder();
sb.append("hello").append("world");
sb.toString();
String.join - If the strings to be concatenated is in the form of array, its better to use String.join rather than looping through the array and appending using a StringBuilder, which String.join does it already inernally. If the array passed is null
, it throws NullPointerException
.
String a = {"hello", "world"};
String.join("", a)
StringUtils.join - If the strings to be concatenated is in the form of array, this can also be used. It internally uses StringBuilder. But just for string concatenation there is no need to include a jar. It precalcualtes the capacity
of the StringBuilder object based on the numnber of elements in the array. If the array passed is null
, it doesn't throws exception but just returns null
string.
String a = {"hello", "world"};
StringUtils.join(a, "")
answered Nov 8 at 19:41
Vignesh Raja
2,6741619
2,6741619
add a comment |
add a comment |
up vote
0
down vote
A few tips:
- StringBuffer - when working with many strings
- StringBuilder - if you use String concatenation in a loop
- String.concat - only when you have to concat two string variables.
2
StringBuffer: if you use String concatenation in a loop from multiple threads (it is basically just a synchronized StringBuilder); in other words, basically never. (Or to pass to an API that was designed before StringBuilder, and was never retrofitted to accept StringBuilder).
– Andy Turner
Nov 8 at 19:06
add a comment |
up vote
0
down vote
A few tips:
- StringBuffer - when working with many strings
- StringBuilder - if you use String concatenation in a loop
- String.concat - only when you have to concat two string variables.
2
StringBuffer: if you use String concatenation in a loop from multiple threads (it is basically just a synchronized StringBuilder); in other words, basically never. (Or to pass to an API that was designed before StringBuilder, and was never retrofitted to accept StringBuilder).
– Andy Turner
Nov 8 at 19:06
add a comment |
up vote
0
down vote
up vote
0
down vote
A few tips:
- StringBuffer - when working with many strings
- StringBuilder - if you use String concatenation in a loop
- String.concat - only when you have to concat two string variables.
A few tips:
- StringBuffer - when working with many strings
- StringBuilder - if you use String concatenation in a loop
- String.concat - only when you have to concat two string variables.
answered Nov 8 at 19:03
Centos
17519
17519
2
StringBuffer: if you use String concatenation in a loop from multiple threads (it is basically just a synchronized StringBuilder); in other words, basically never. (Or to pass to an API that was designed before StringBuilder, and was never retrofitted to accept StringBuilder).
– Andy Turner
Nov 8 at 19:06
add a comment |
2
StringBuffer: if you use String concatenation in a loop from multiple threads (it is basically just a synchronized StringBuilder); in other words, basically never. (Or to pass to an API that was designed before StringBuilder, and was never retrofitted to accept StringBuilder).
– Andy Turner
Nov 8 at 19:06
2
2
StringBuffer: if you use String concatenation in a loop from multiple threads (it is basically just a synchronized StringBuilder); in other words, basically never. (Or to pass to an API that was designed before StringBuilder, and was never retrofitted to accept StringBuilder).
– Andy Turner
Nov 8 at 19:06
StringBuffer: if you use String concatenation in a loop from multiple threads (it is basically just a synchronized StringBuilder); in other words, basically never. (Or to pass to an API that was designed before StringBuilder, and was never retrofitted to accept StringBuilder).
– Andy Turner
Nov 8 at 19:06
add a comment |
2
This question seems impossibly broad. The best one to use depends on the use case. What use cases do you have in mind?
– Daniel Pryden
Nov 8 at 18:47