Re: Which is more efficient?

From: metfan (qjzhupublic_at____NoSpam__yahoo.ie)
Date: 10/24/03


Date: Fri, 24 Oct 2003 16:21:25 +0800


>> For the following to snips, which one do you think is more efficient?
>>
>> snip1:
>> BufferedWriter bw = new BufferedWriter(new
>> OutputStreamWriter(System.out));
>> bw.write("abc" + "def");
>>
>> snip2:
>> BufferedWriter bw = new BufferedWriter(new
>> OutputStreamWriter(System.out));
>> bw.write("abc");
>> bw.write("def");
>
> The first, as it compiles to the same as:
>
> BufferedWriter bw = new BufferedWriter(new
> OutputStreamWriter(System.out));
> bw.write("abcdef");
>
> because literal strings are concatenated at compile time.
>

   Well.. what if:

snip1:
          BufferedWriter bw = new BufferedWriter(new
OutputStreamWriter(System.out));
        String a = new String("abc");
        String b = new String("def");
          bw.write(a + b);

snip2:
          BufferedWriter bw = new BufferedWriter(new
OutputStreamWriter(System.out));
        String a = new String("abc");
        String b = new String("def");
          bw.write(a);
          bw.write(b);

-- 
Best regards,
Qingjia Zhu
Q@z@J


Relevant Pages

  • Re: Compile time string literal substitution to external data file
    ... > literal strings, and then building a tool to strip the strings from the ... > binary at compile time. ... I noticed one community member has posted a response to the ...
    (microsoft.public.vc.language)
  • Re: String question
    ... Lew wrote: ... literal strings at compile time, ... It has to, by JLS 15.28, "Constant Expression". ...
    (comp.lang.java.programmer)
  • Re: Which is more efficient?
    ... BufferedWriter bw = new BufferedWriter(new ... because literal strings are concatenated at compile time. ... If replying to the group, please do not mail me too ...
    (comp.lang.java.programmer)