private static final char DECIMAL_PLACE = '.';
private static final int REPEATS = 10000;
public static void main(String... args) {
doTest();
doTest();
doTest();
}
private static void doTest() {
long start = System.nanoTime();
String s = null;
for (int i = 0; i < REPEATS; i++)
s = "" + DECIMAL_PLACE;
long time = System.nanoTime() - start;
assert ".".equals(s);
start = System.nanoTime();
for (int i = 0; i < REPEATS; i++)
s = String.valueOf(DECIMAL_PLACE);
assert ".".equals(s);
long time2 = System.nanoTime() - start;
System.out.println("\"\"+DECIMAL_PLACE time=" + time + " ns");
System.out.println("String.valueOf(DECIMAL_PLACE) time=" + time2 + " ns");
}
I guess this inspection should not warn on compile time constants.