class test
{
}
Compile it (with -O if you want to test the optimizer (which is mostly non-existant from what I understand)) and run javap on it (output below). As you can see, the code is not quite as optimized as you originally hoped, there are two StringBuffer's individually instantiated. However, note that the second version I added (one String version with the three strings constants concatenated in a single statement) is concatenated at compile time and turns into much more efficient code. Note that this only works for string _constants_, if you are also working with variables it does create StringBuffers. I leave that as an exercise of your own "javap -c" :)
enjoy!
Jason Reich.
C:\TEMP>javap -c test
Compiled from test.java
class test extends java.lang.Object {
test();
public void test();
}
Method test()
0 aload_0
1 invokespecial #11 <Method java.lang.Object()>
4 return
Method void test()
0 ldc #2 <String "SELECT COF_NAME, ">
2 astore_1
3 new #8 <Class java.lang.StringBuffer>
6 dup
7 aload_1
8 invokestatic #17 <Method java.lang.String valueOf(java.lang.Object)>
11 invokespecial #12 <Method java.lang.StringBuffer(java.lang.String)>
14 ldc #1 <String "SALES FROM COFFEES ">
16 invokevirtual #13 <Method java.lang.StringBuffer append(java.lang.String)>
19 invokevirtual #16 <Method java.lang.String toString()>
22 astore_1
23 new #8 <Class java.lang.StringBuffer>
26 dup
27 aload_1
28 invokestatic #17 <Method java.lang.String valueOf(java.lang.Object)>
31 invokespecial #12 <Method java.lang.StringBuffer(java.lang.String)>
34 ldc #4 <String "WHERE COF_NAME = 'Starbucks'">
36 invokevirtual #13 <Method java.lang.StringBuffer append(java.lang.String)>
39 invokevirtual #16 <Method java.lang.String toString()>
42 astore_1
43 getstatic #14 <Field java.io.PrintStream out>
46 aload_1
47 invokevirtual #15 <Method void println(java.lang.String)>
50 ldc #3 <String "SELECT COF_NAME, SALES FROM COFFEES WHERE COF_NAME = 'Starbucks'">
52 astore_1
53 getstatic #14 <Field java.io.PrintStream out>
56 aload_1
57 invokevirtual #15 <Method void println(java.lang.String)>
60 return
At 12:42 PM 7/5/00 -0700, Man Chi Ly wrote:
On Wed, 5 Jul 2000, Tony J. Paul wrote:
> Hi brEezE,
>
> I am new to this list. I don't know if this will suffice your
> requirement. Anyway, Why don't you try StringBuffer class? You can use
> it like this,
>
> StringBuffer sb=new StringBuffer();
> sb.append("SELECT COF_NAME, ");
> sb.append("SALES FROM COFFEES ");
> stmt.executeQuery(sb.toString());
>
I recall reading somewhere that the following code is optimized by javac
(roughly) into the form you provided just above:
String s;
s = "SELECT COF_NAME, ";
s += "SALES FROM COFFEES ");
s += "WHERE COF_NAME = 'Starbucks'";
stmt.executeQuery(s);
In other words, the compiler optimizes successive calls to the += operator
into StringBuffer.append() calls without creating a bunch of intermediate
objects. Does javac actually do this, or do I need to go back to the ugly
StringBuffer idiom for efficiency?
> Moreover, the "+" sign is actually over loaded in JAVA. Internally,
> for every "+" a string buffer is instantiated and does what I have shown
> above. So using string buffer would be more efficient.
>
> regards
>
> Tony P.
>
>
>
> brEezE wrote:
>
> > Hi all, I have a dummy question regaring a
> > double-quoted string written in multiple line.
> > Here is what I have been doing:
> > stmt.executeQuery( "SELECT COF_NAME, " +
> > "SALES FROM COFFEES " );
> >
> > Is there a way to to eliminate the '+' sign by doing
> > something like below?
> >
> > stmt.executeQuery( "SELECT COF_NAME, \
> > SALES FROM COFFEES " );
> >
> >
> > I know the code cannot be compiled, but is there any
> > substitution to the escape character above?
> >
> > Many thanks in advance.
> >
> > __________________________________________________
> > Do You Yahoo!?
> > Kick off your party with Yahoo! Invites.
> > http://invites.yahoo.com/
> >
> >
> > ----------------------------------------------------------------------
> > To UNSUBSCRIBE, email to [EMAIL PROTECTED]
> > with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]
> >
>
> --
> -------------------------------------------------------------------------
>
> Tony Judy Paul,
>
> Software Engineer,
>
> AdventNet Development Centre (I) Pvt.Ltd,
>
> 13A, Kaashyap Enclave,
>
> Velacherry Main Road,
>
> Velacherry.
>
> Chennai-600042
>
> E-mail: [EMAIL PROTECTED]
>
> Phone: 2432414-419
>
> -------------------------------------------------------------------------
>
>
>
----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]