Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2007

Tip: Looking for answers? Try searching our database.

Multiline strings?

Thread view: 
Vadim Tropashko - 08 May 2007 17:40 GMT
Is there a convenient way to have multiline text strings assigned in
java? Something like:

String sqlText = "
  select empno, dname, sal from emp, dept
  where emp.deptno = dept.deptno
  order by sal
";

I came across a project where Stings like this are extracted into a
separate XML file (with all associated XML horrors).

I remember asking this question sometime a decade ago, do things
change:-?
Steve W. Jackson - 08 May 2007 18:56 GMT
> Is there a convenient way to have multiline text strings assigned in
> java? Something like:
[quoted text clipped - 10 lines]
> I remember asking this question sometime a decade ago, do things
> change:-?

For a literal string as in your example, break it into pieces and use
the "+" operator to concatenate the parts.

It's not a good idea to do this on any large scale in running code (as
in concatenating literals to String variables, for instance) since it
can cause performance and/or memory issues.  That's why you have the
StringBuilder and StringBuffer classes.  But for compile-time literals,
it shouldn't pose any problems.

= Steve =
Signature

Steve W. Jackson
Montgomery, Alabama

Vadim Tropashko - 08 May 2007 19:46 GMT
On May 8, 10:56 am, "Steve W. Jackson" <stevewjack...@knology.net>
wrote:
> In article <1178642435.306145.301...@n59g2000hsh.googlegroups.com>,
>
[quoted text clipped - 12 lines]
> > I remember asking this question sometime a decade ago, do things
> > change:-?

> It's not a good idea to do this on any large scale in running code (as
> in concatenating literals to String variables, for instance) since it
> can cause performance and/or memory issues.  That's why you have the
> StringBuilder and StringBuffer classes.  But for compile-time literals,
> it shouldn't pose any problems.

The

String a += b + c;

part is routinely optimized to StringBuffer. (It is optimization bug
if it is not yet).

OK, on afterthought it seems to be merely a compiler issue. One must
have to redefine string literal in lexer rules.
Tom Hawtin - 08 May 2007 22:45 GMT
> String a += b + c;

You mean "a += b + c;" or "String a = b + c + d;"?

> part is routinely optimized to StringBuffer. (It is optimization bug
> if it is not yet).

Pessimised, actually. Assuming a, b and c really are String objects,

   a = a.concat(b).concat(c);

is likely to be faster (from 1.5 at least).

Tom Hawtin


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.