File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String assignment confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String assignment confusion" Watch "String assignment confusion" New topic

String assignment confusion

Arthur Blair
Ranch Hand

Joined: Sep 20, 2005
Posts: 71

Why do these three compile successfully:

But this: cause a compile time error saying "unclosed string literal".
Madhu Iyer

Joined: Dec 27, 2005
Posts: 4
The same error is repeated for :
String a="\u000a";

don't know why
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2398
The problem is what that UNICODE represents. 000d is carriage return and 000a is new line. These cannot be represented as character literals. You have to represent them with \r and \n
ven kaar
Ranch Hand

Joined: Nov 01, 2005
Posts: 39
\u000a line feed
\u000d carriage return

Parallax - Change in observational position that provides a new line of sight.
Vincent Brabant
Ranch Hand

Joined: Nov 01, 2004
Posts: 33
It's because \u000a and \u000d are corresponding to CR and LF.
And translation of unicode caracters is done before compilation.
So, it means that compiler will first read your code and transform


and then will give a compilation error because, for the compiler, brackets must be closed on the same line, not the next one.

It's very well explained in the Java Language Specification (see � 3.10.4 )


Vincent Brabant<br /><a href="" target="_blank" rel="nofollow"></a>
Arthur Blair
Ranch Hand

Joined: Sep 20, 2005
Posts: 71
ok, understood.

So what about these:

A to E each set the char to 1.

what does the \ character mean in this context?

I'm guessing that with the \u the four digits that follow represent a unicode character, right? (char e = '\u0031' ; )

But what about (char b = '\61' ; ) and (char f = '\7' ; ) and the ones that don't compile?
[ January 31, 2006: Message edited by: Arthur Blair ]
ven kaar
Ranch Hand

Joined: Nov 01, 2005
Posts: 39
I Guess
\b \r \n \t \f \' \" \\ are the valid excape sequence in java, rest are invalid ??? hope the below helps

3.10.6 Escape Sequences for Character and String Literals
The character and string escape sequences
I agree. Here's the link:
subject: String assignment confusion
jQuery in Action, 3rd edition