This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String assignment confusion" Watch "String assignment confusion" New topic
Author

String assignment confusion

Arthur Blair
Ranch Hand

Joined: Sep 20, 2005
Posts: 71
Ok.

Why do these three compile successfully:


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

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: 2367
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



into


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
http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.10.4 )

Vincent


Vincent Brabant<br /><a href="http://fr.netbeans.org" target="_blank" rel="nofollow">http://fr.netbeans.org</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

http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html

3.10.6 Escape Sequences for Character and String Literals
The character and string escape sequences
 
Don't get me started about those stupid light bulbs.
 
subject: String assignment confusion
 
Similar Threads
convert given unicode literal
String
Unicode problem
character literals
how to convert unicode to string