Videos
Since String is immutable in java, when you do a +, += or concat(String), a new String is generated. The bigger the String gets the longer it takes - there is more to copy and more garbage is produced.
Today's java compilers optimizes your string concatenation to make it optimal, e.g.
System.out.println("x:"+x+" y:"+y);
Compiler generates it to:
System.out.println((new StringBuilder()).append("x:").append(x).append(" y:").append(y).toString());
My advice is to write code that's easier to maintain and read.
This link shows performance of StringBuilder vs StringBuffer vs String.concat - done right
It shouldn't matter. Modern day Java compilers, JVMs and JITs will optimize your code in such a way that differences are likely to be minimal. You should strive to write code that's more readable and maintainable for you.
varags. If a method signature is method(Param param, String... x) it will take one Param type of object and any number of String objects.
There are couple if cool things about it:
It's nothing special. It's just sugared array. So,
method(MyObject... o)is same asmethod(MyObject[] o).Vararg has to be the last parameter in argument list.
There is this funny thing that bit me once.
method(MyObject... o)can be called asmethod()without any compilation error. Java will internally convert the no-arg call tomethod(new MyObject[0]). So, be aware of this.
It's syntax for writing the items of the array as a parameter
for instance:
public String first (String... values) {
return values[0];
}
Then you can call this method with first("4","2","5","67")
The javacompiler will create an array of the parameters on its own.