I have a simple question regarding optimization. Let's say I have a function that should return a String with one thousand 'A' characters (the number is arbitrary and might be parametrized later). What would be the best way to go around doing so?
StringBuilder sb = new StringBuilder();
for(int i=0; i<1000; i++)
sb.append('A');
return sb.toString();
I've come up with this solution utilizing StringBuilder but I feel there should be a better solution, a memcpy of sorts.
Thanks!
Demo:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
char[] arr = new char[1000];
Arrays.fill(arr, 'A');
String str = new String(arr);
System.out.println(str);
}
}
Demo:
public class Main {
public static void main(String[] args) {
String str = "A".repeat(1000);
System.out.println(str);
}
}