Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > String literals in Java

Reply
Thread Tools

String literals in Java

 
 
Harri Pesonen
Guest
Posts: n/a
 
      05-27-2004
I'm new to Java and find Java string literals a bit limited. In C/C++ it
is possible to concatenate string literals by doing "ab" "cde", and in
Python it is possible to have raw strings like r"C:\Windows" where \
does not need to be doubled (good for regular expressions as well).
Python also has multi-line strings like """
SELECT *
FROM COMPANY
WHERE NAME LIKE 'MIC%'
""" which is good for SQL, for example. All these are missing from Java.
Has this been discussed, and are any improvements coming in Java 1.5
(which has a lot of other good features that make programming a lot
easier like auto-boxing of primitive types)?

Harri
 
Reply With Quote
 
 
 
 
Ryan Stewart
Guest
Posts: n/a
 
      05-27-2004
"Harri Pesonen" <(E-Mail Removed)> wrote in message
news:_Uutc.4323$(E-Mail Removed).. .
> I'm new to Java and find Java string literals a bit limited. In C/C++ it
> is possible to concatenate string literals by doing "ab" "cde", and in

"ab" + "cde"

> Python it is possible to have raw strings like r"C:\Windows" where \
> does not need to be doubled (good for regular expressions as well).

Then how do you get a " in a raw string? It could be useful, but I feel it
would be an unnecessary complication.

> Python also has multi-line strings like """
> SELECT *
> FROM COMPANY
> WHERE NAME LIKE 'MIC%'
> """ which is good for SQL, for example. All these are missing from Java.

String sql = "SELECT * "
+ "FROM company "
+ "WHERE name LIKE 'MIC%'";


 
Reply With Quote
 
 
 
 
Harri Pesonen
Guest
Posts: n/a
 
      05-27-2004
Ryan Stewart wrote:

> "Harri Pesonen" <(E-Mail Removed)> wrote in message
> news:_Uutc.4323$(E-Mail Removed).. .
>
>>I'm new to Java and find Java string literals a bit limited. In C/C++ it
>>is possible to concatenate string literals by doing "ab" "cde", and in

>
> "ab" + "cde"


"ab" "cde" is better, because it would happen in compile time.

>>Python it is possible to have raw strings like r"C:\Windows" where \
>>does not need to be doubled (good for regular expressions as well).

>
> Then how do you get a " in a raw string? It could be useful, but I feel it
> would be an unnecessary complication.


It was introduced in Python just for regular expressions I believe. It
is not fun to write s.replaceAll("\\\\", "\\\\\\\\") in Java either.


>>Python also has multi-line strings like """
>>SELECT *
>>FROM COMPANY
>>WHERE NAME LIKE 'MIC%'
>>""" which is good for SQL, for example. All these are missing from Java.

>
> String sql = "SELECT * "
> + "FROM company "
> + "WHERE name LIKE 'MIC%'";


Yeah, but if you have written a big SQL statement in Query Analyzer, and
just want to copy it to Java sources, you have to add all these extra
concatenation operators and string terminators. Not fun. And, the
resulting string does not have line feeds (in your example). You have to
add \n in the end of each line to get the same string.

Harri
 
Reply With Quote
 
Carl Howells
Guest
Posts: n/a
 
      05-28-2004
Harri Pesonen wrote:
> Ryan Stewart wrote:
>
>> "Harri Pesonen" <(E-Mail Removed)> wrote in message
>> news:_Uutc.4323$(E-Mail Removed).. .
>>
>>> I'm new to Java and find Java string literals a bit limited. In C/C++ it
>>> is possible to concatenate string literals by doing "ab" "cde", and in

>>
>>
>> "ab" + "cde"

>
>
> "ab" "cde" is better, because it would happen in compile time.


First, unless you're doing it in a loop, it's just not very important.

Second, it does happen at compile time, as long as all the pieces being
concatenated are literals.
 
Reply With Quote
 
Ryan Stewart
Guest
Posts: n/a
 
      05-28-2004
"Carl Howells" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Harri Pesonen wrote:
> > Ryan Stewart wrote:
> >
> >> "Harri Pesonen" <(E-Mail Removed)> wrote in message
> >> news:_Uutc.4323$(E-Mail Removed).. .
> >>
> >>> I'm new to Java and find Java string literals a bit limited. In C/C++

it
> >>> is possible to concatenate string literals by doing "ab" "cde", and in
> >>
> >>
> >> "ab" + "cde"

> >
> >
> > "ab" "cde" is better, because it would happen in compile time.

>
> First, unless you're doing it in a loop, it's just not very important.
>
> Second, it does happen at compile time, as long as all the pieces being
> concatenated are literals.


Besides which, if they're just literals, wouldn't you rather write "abcde"
anyway?


 
Reply With Quote
 
Tony Morris
Guest
Posts: n/a
 
      05-28-2004
> First, unless you're doing it in a loop, it's just not very important.
>
> Second, it does happen at compile time, as long as all the pieces being
> concatenated are literals.


Second, it does happen at compile time, as long as all the pieces being
concatenated are "compile-time constants".

final char c = 'h';
// c is compile-time constant, but not literal
String s = c + "ello";

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform



 
Reply With Quote
 
Ryan Stewart
Guest
Posts: n/a
 
      05-28-2004
"Harri Pesonen" <(E-Mail Removed)> wrote in message
news:d6vtc.4324$(E-Mail Removed).. .
> Ryan Stewart wrote:
> > "Harri Pesonen" <(E-Mail Removed)> wrote in message
> > news:_Uutc.4323$(E-Mail Removed).. .
> >>Python it is possible to have raw strings like r"C:\Windows" where \
> >>does not need to be doubled (good for regular expressions as well).

> >
> > Then how do you get a " in a raw string? It could be useful, but I feel

it
> > would be an unnecessary complication.

>
> It was introduced in Python just for regular expressions I believe. It
> is not fun to write s.replaceAll("\\\\", "\\\\\\\\") in Java either.
>

I'll agree with you there

> >>Python also has multi-line strings like """
> >>SELECT *
> >>FROM COMPANY
> >>WHERE NAME LIKE 'MIC%'
> >>""" which is good for SQL, for example. All these are missing from Java.

> >
> > String sql = "SELECT * "
> > + "FROM company "
> > + "WHERE name LIKE 'MIC%'";

>
> Yeah, but if you have written a big SQL statement in Query Analyzer, and
> just want to copy it to Java sources, you have to add all these extra
> concatenation operators and string terminators. Not fun. And, the
> resulting string does not have line feeds (in your example). You have to
> add \n in the end of each line to get the same string.
>

Some IDE's will do the multiline concatenation stuff for you, and what would
you want with newlines in SQL?


 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      05-28-2004
On Fri, 28 May 2004 02:33:19 +0300, Harri Pesonen <(E-Mail Removed)>
wrote or quoted :

>""" which is good for SQL, for example. All these are missing from Java.
>Has this been discussed, and are any improvements coming in Java 1.5
>(which has a lot of other good features that make programming a lot


You can create any literal you want in Java. See
http://mindprod.com/jgloss/literals.html


Concatenation needs explicit +. Two literals are combined at compile
time, so there is no speed penalty for the split.

The biggest pain are in regexes where literal \ must be encoded as
\\\\

Sun generally refuses to add sugar to the language -- short forms that
generate the same code as long hand. The relented slightly with the
new for loops. Perhaps other like short hand Listeners will come,
then maybe the sort of things you want. I would not hold by breath.

I think you will get them via scids rather than language evolution.

See http://mindprod.com/jgloss/scid.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Liz
Guest
Posts: n/a
 
      05-28-2004
maybe there are some other languages like lisp and ada and such
that have features you like too........
maybe java is just too hard for you......


"Harri Pesonen" <(E-Mail Removed)> wrote in message
news:_Uutc.4323$(E-Mail Removed).. .
> I'm new to Java and find Java string literals a bit limited. In C/C++ it
> is possible to concatenate string literals by doing "ab" "cde", and in
> Python it is possible to have raw strings like r"C:\Windows" where \
> does not need to be doubled (good for regular expressions as well).
> Python also has multi-line strings like """
> SELECT *
> FROM COMPANY
> WHERE NAME LIKE 'MIC%'
> """ which is good for SQL, for example. All these are missing from Java.
> Has this been discussed, and are any improvements coming in Java 1.5
> (which has a lot of other good features that make programming a lot
> easier like auto-boxing of primitive types)?
>
> Harri



 
Reply With Quote
 
Harri Pesonen
Guest
Posts: n/a
 
      05-28-2004
Ryan Stewart wrote:

> "Harri Pesonen" <(E-Mail Removed)> wrote in message
> news:d6vtc.4324$(E-Mail Removed).. .
>
>>Ryan Stewart wrote:
>>
>>>"Harri Pesonen" <(E-Mail Removed)> wrote in message
>>>news:_Uutc.4323$(E-Mail Removed) t...
>>>
>>>>Python also has multi-line strings like """
>>>>SELECT *
>>>
>>>>FROM COMPANY
>>>
>>>>WHERE NAME LIKE 'MIC%'
>>>>""" which is good for SQL, for example. All these are missing from Java.
>>>
>>>String sql = "SELECT * "
>>> + "FROM company "
>>> + "WHERE name LIKE 'MIC%'";

>>
>>Yeah, but if you have written a big SQL statement in Query Analyzer, and
>>just want to copy it to Java sources, you have to add all these extra
>>concatenation operators and string terminators. Not fun. And, the
>>resulting string does not have line feeds (in your example). You have to
>>add \n in the end of each line to get the same string.
>>

>
> Some IDE's will do the multiline concatenation stuff for you, and what would
> you want with newlines in SQL?


It is nice to have SQL formatted in readable form, in all phases: Query
Analyzer, Java source, Profiler, log file. """ is raw string as well, so
that \ does not need to be doubled, and it can contain tab characters as
well. It would be really great for database development. Of course, if
you have some data-aware control/component, you can write the SQL there.
But unfortunately you can't do it in Java.

You said that some IDEs will do the multiline concatenation? Which ones
and how? Not NetBeans? Still if IDE has some tool to make the
concatenation, then the SQL does not remain in readable form anymore,
but this would be better than nothing.

Harri
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
32-bit characters in Java string literals Roedy Green Java 13 12-28-2009 08:39 PM
java compiler and string literals John and Diane Curley Java 5 05-21-2006 10:53 AM
Java: byte literals and short literals John Goche Java 8 01-17-2006 11:12 PM
Generic class literals - e.g,, Class<Map<String, Integer>>.class Purush Java 4 04-13-2005 08:40 PM
character literals and string Pete Elmgreen Java 3 11-24-2004 04:42 PM



Advertisments