Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String assignment confusion

 
Arthur Blair
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok.

Why do these three compile successfully:


But this: cause a compile time error saying "unclosed string literal".
 
Madhu Iyer
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The same error is repeated for :
String a="\u000a";


don't know why
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
\u000a line feed
\u000d carriage return
 
Vincent Brabant
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Arthur Blair
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic