Multiline strings?

  • Thread starter Vadim Tropashko
  • Start date
V

Vadim Tropashko

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:-?
 
S

Steve W. Jackson

Vadim Tropashko said:
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:-?

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 =
 
V

Vadim Tropashko

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.
 
T

Tom Hawtin

Vadim said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,241
Latest member
Lisa1997

Latest Threads

Top