Priority
Normal
Type
Bug 
State
Fixed 
Assignee
Bas Leijdekkers 
Subsystem
Code Analysis. Inspection 
Affected versions
Fixed in
Fixed in build
Next build 
Build
7531
Fixed in build
7719
  • Submitted by   Peter Lawrey
    2 years ago (12 Nov 2007 17:33)
  • Updated by   root
    3 weeks ago (17 Jan 2010 19:55)
  • Jira: IDEA-41914
    (history, comments)

IDEA-41914

"Concatenation with empty string" inspection should not warn on compile time constants

0
It notes: "Such concatenation is unnecessary and inefficient, particularly when used as an idiom for formatting non-String objects or primitives into Strings. "

However in the following test you can see that using ""+X is many times faster that String.valueOf(X) as the compiler can convert the string at compile time. Perhaps it should read "Such concatenation is necessary and efficient ..."

Why is it use at all, to avoid defining two constants, one as a char and the other as a String.

""+DECIMAL_PLACE time=151659 ns
String.valueOf(DECIMAL_PLACE) time=9442612 ns
""+DECIMAL_PLACE time=44825 ns
String.valueOf(DECIMAL_PLACE) time=2457340 ns
""+DECIMAL_PLACE time=30193 ns
String.valueOf(DECIMAL_PLACE) time=1334597 ns


the code is
    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");
    }


Issue was resolved
Comments (1)
 
History (1)
 
Links
 
Bas Leijdekkers
  Bas Leijdekkers
12 Nov 2007 18:32
(2 years ago)
#permalink
For constants the concatenation with empty string is indeed better, because the conversion can be done at compile time. For runtime values the inspection is correct:
private static final char DECIMAL_PLACE = '.';
	private static final int REPEATS = 10000;

	public static void main(String... args) {
		doTest(DECIMAL_PLACE);
		doTest(DECIMAL_PLACE);
		doTest(DECIMAL_PLACE);
	}

	private static void doTest(char c) {
		long start = System.nanoTime();
		String s = null;
		for (int i = 0; i < REPEATS; i++)
			s = "" + c;
		long time = System.nanoTime() - start;
		assert ".".equals(s);
		start = System.nanoTime();
		for (int i = 0; i < REPEATS; i++)
			s = String.valueOf(c);
		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.