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-28-2004
Hehe, right, lisp...

Java is harder than Python in many ways. On the other hand, Java has
code completion and compile time type checking. And fortunately Java 1.5
has great improvements like primitive type auto-boxing and finally a
string "replace" function that works as expected.

Harri

Liz wrote:

> 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
 
 
 
 
Ryan Stewart
Guest
Posts: n/a
 
      05-28-2004
"Harri Pesonen" <(E-Mail Removed)> wrote in message
news:30Btc.4351$%(E-Mail Removed).. .
> Ryan Stewart wrote:
> > 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.
>

True, but at least all it takes is a simple System.out.println or log.debug
to see if your SQL is correct.

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

I've been mainly using NetBeans for several months, and am not aware of the
option for that. I think IntelliJ does, but it's not free. In JDeveloper, if
there's not an option for it, you can create a code template for it.


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

> "Harri Pesonen" <(E-Mail Removed)> wrote in message
> news:30Btc.4351$%(E-Mail Removed).. .
>
>>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.

>
> True, but at least all it takes is a simple System.out.println or log.debug
> to see if your SQL is correct.


I have to correct myself. In Python, """ can have escape sequences like
any normal string. Only r" has limited escape sequences:

"When an "r" or "R" prefix is present, a character following a backslash
is included in the string without change, and all backslashes are left
in the string. For example, the string literal r"\n" consists of two
characters: a backslash and a lowercase "n". String quotes can be
escaped with a backslash, but the backslash remains in the string; for
example, r"\"" is a valid string literal consisting of two characters: a
backslash and a double quote; r"\" is not a valid string literal (even a
raw string cannot end in an odd number of backslashes). Specifically, a
raw string cannot end in a single backslash (since the backslash would
escape the following quote character). Note also that a single backslash
followed by a newline is interpreted as those two characters as part of
the string, not as a line continuation."

So r" string can have " in it. Also:

"When an "r" or "R" prefix is used in conjunction with a "u" or "U"
prefix, then the \uXXXX escape sequence is processed while all other
backslashes are left in the string. For example, the string literal
ur"\u0062\n" consists of three Unicode characters: `LATIN SMALL LETTER
B', `REVERSE SOLIDUS', and `LATIN SMALL LETTER N'. Backslashes can be
escaped with a preceding backslash; however, both remain in the string.
As a result, \uXXXX escape sequences are only recognized when there are
an odd number of backslashes. "

"In triple-quoted strings, unescaped newlines and quotes are allowed
(and are retained), except that three unescaped quotes in a row
terminate the string. (A ``quote'' is the character used to open the
string, i.e. either ' or ".)"

All this is a bit confusing compared to how C# handles this:

"A verbatim string literal consists of an @ character followed by a
double-quote character, zero or more characters, and a closing
double-quote character. A simple example is @"hello". In a verbatim
string literal, the characters between the delimiters are interpreted
verbatim, the only exception being a quote-escape-sequence. In
particular, simple escape sequences and hexadecimal and Unicode escape
sequences are not processed in verbatim string literals. A verbatim
string literal may span multiple lines."

This sounds good, except that " has to be doubled. The ideal Java
verbatim string would be something like:

String s = @"can have anything here,
including line feeds, file paths like:
C:\Windows
even tabulators. How to embed " then? Simple, the terminating " must
have @ after it."@;

This way the only string sequence that is now allowed is the closing "@.

Perhaps there could be better terminators, like {"How about this then?"}
or <"And this?">. There are also a couple of characters that are not
normally used: ´Would this be cool?´ `Probably not.`

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

>
>Perhaps there could be better terminators, like {"How about this then?"}
>or <"And this?">. There are also a couple of characters that are not
>normally used: ´Would this be cool?´ `Probably not.`


The generic solution would be to allow you to choose the terminator:
e.g.
@$abc$

That still does not deal with the problem of embedded escapes.

I'm running into this problem with macros. I don't want to fool with
quoting accidentally embedded terminators, so I have allowed a variety
of enclosers.
--
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
 
Harri Pesonen
Guest
Posts: n/a
 
      05-28-2004
Roedy Green wrote:

> On Fri, 28 May 2004 19:01:07 +0300, Harri Pesonen <(E-Mail Removed)>
> wrote or quoted :
>
>>Perhaps there could be better terminators, like {"How about this then?"}
>>or <"And this?">. There are also a couple of characters that are not
>>normally used: ´Would this be cool?´ `Probably not.`

>
> The generic solution would be to allow you to choose the terminator:
> e.g.
> @$abc$
>
> That still does not deal with the problem of embedded escapes.
>
> I'm running into this problem with macros. I don't want to fool with
> quoting accidentally embedded terminators, so I have allowed a variety
> of enclosers.


Btw, there is a RFE for this, please vote for it...

http://bugs.sun.com/bugdatabase/view...bug_id=4472509

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

> Btw, there is a RFE for this, please vote for it...
>
> http://bugs.sun.com/bugdatabase/view...bug_id=4472509


The problem with Sun's RFE system is that there doesn't seem to be a way to
vote /against/ things.

-- chris


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

> Harri Pesonen wrote:
>
>>Btw, there is a RFE for this, please vote for it...
>>
>>http://bugs.sun.com/bugdatabase/view...bug_id=4472509

>
> The problem with Sun's RFE system is that there doesn't seem to be a way to
> vote /against/ things.


Why would anyone want that Java is not improved? Java 1.5 focuses on
"Ease of Development". It does it by taking many things from C#:

* auto-boxing of primitives
* metadata (attributes)
* enumerated types
* Enhanced for Loop (foreach)
* formatted output (String.Format)
* Varargs (parameter arrays)

Verbatim string literals is one thing that is still missing.

Harri
 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      05-28-2004
> Chris Uppal wrote:
> > The problem with Sun's RFE system is that there doesn't seem to be a way to
> > vote /against/ things.


Harri Pesonen wrote:
> Why would anyone want that Java is not improved?


Circular answer. The word "improved" implies that the change is for the
better. Obviously, Chris doesn't think so. Not thinking so is a
reasonable opinion. Unbounded increases in the complexity of a language
definition will kill the language. Every change has to be weighed
against the increase in language complexity. This change doesn't add
too awfully much complexity, but it doesn't provide much benefit either.

> Java 1.5 focuses on
> "Ease of Development". It does it by taking many things from C#:


And hence everything from C# is good by default? Go write code in C#,
then.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
Harri Pesonen
Guest
Posts: n/a
 
      05-28-2004
Chris Smith wrote:

>>Chris Uppal wrote:
>>
>>>The problem with Sun's RFE system is that there doesn't seem to be a way to
>>>vote /against/ things.

>
> Harri Pesonen wrote:
>
>>Why would anyone want that Java is not improved?

>
> Circular answer. The word "improved" implies that the change is for the
> better. Obviously, Chris doesn't think so. Not thinking so is a
> reasonable opinion. Unbounded increases in the complexity of a language
> definition will kill the language. Every change has to be weighed
> against the increase in language complexity. This change doesn't add
> too awfully much complexity, but it doesn't provide much benefit either.


Ask anybody that does SQL development. Also adding verbatim string to
java compiler would be trivial.

>>Java 1.5 focuses on
>>"Ease of Development". It does it by taking many things from C#:

>
> And hence everything from C# is good by default? Go write code in C#,
> then.


C# is technically better than Java, because it is a next generation
product. Now Java tries to catch up. If C# and .NET were available on
every major platform (Win, Linux, Mac) for free, and if there were free
development systems (like NetBeans), I would be using C#. As it is, Java
is better for multiplatform development, and it is free, so there is no
choice (unfortunately).

Harri
 
Reply With Quote
 
Jim Cochrane
Guest
Posts: n/a
 
      05-28-2004
In article <laOtc.4545$(E-Mail Removed)>, Harri Pesonen wrote:
> Chris Smith wrote:
>
>>>Chris Uppal wrote:
>>>
>>>>The problem with Sun's RFE system is that there doesn't seem to be a way to
>>>>vote /against/ things.

>>
>> ...

>
> C# is technically better than Java, because


When I read this I thought: "Oh, good - I don't know much about C#, maybe
I'll learn something about why it may be better than Java." But then I
read this: "it is a next generation product." and thought: "How
disappointing - He does not really give a reason, but just uses an
essentially meaningless phrase. He doesn't even give any kind of
definition for 'next generation product', which might have at least given
some sense of why C# is technically better than Java."

I guess I didn't learn anything after all.

> product. Now Java tries to catch up. If C# and .NET were available on
> every major platform (Win, Linux, Mac) for free, and if there were free
> development systems (like NetBeans), I would be using C#. As it is, Java
> is better for multiplatform development, and it is free, so there is no
> choice (unfortunately).
>
> Harri



--
Jim Cochrane; http://www.velocityreviews.com/forums/(E-Mail Removed)
[When responding by email, include the term non-spam in the subject line to
get through my spam filter.]
 
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