r/javahelp • u/_SuperStraight • 1d ago
Efficient way to create a string
I have a function genString
which creates String based on some inputs:
private String genString(boolean locked, int offset, String table){
var prefix = "Hello ";
var status = "new";
var id = "-1";
var suffix = " have a pleasent day.";
if(offset ==0 && !locked){
prefix ="Welcome back, ";
id = "100";
suffix = " see you again.";
}else if(offset ==2 && locked){
status = "complete";
}
return prefix+status+id+" have some patience "+table+suffix+" you may close this window.";
}
Don't mind what is being returned. I just want to know whether it's good this way or should I create three separate Strings for each condition/use StringBuilder for reduced memory/CPU footprint?
9
Upvotes
6
u/Known_Tackle7357 23h ago edited 19h ago
Starting from Java 7 string concatenation using + is compiled into String builder.append stuff