• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strange output

 
Udaya Raj
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class ADirtyOne
{
//char a = '\u000A';
}

Why does the compilation error occurs while compiling this program
 
Netty poestel
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I think your panacea lies in:-

http://www.javaranch.com/certfaq.jsp

do a keywd. search for "char a = '\u000A'. Why is this invalid?"

Chow
 
Vishnu Munnangi
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
'\u000A' cannot be used as character. It represents an escape sequence for new line that is '\n'.
so better use the escape sequence '\n' instead of '\u000A'.
 
Marc Winder
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To follow up with what Vishnu said, what happens when you compile it is this;

char a =
;

That is why you are getting a compile error
 
Udaya Raj
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any way I have commented it. Why should the compiler care about the line which is commented
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any way I have commented it. Why should the compiler care about the line which is commented


As was stated above, the compiler translates \u000A into a line feed in your source program, so the last two characters are on the next line and not part of the comment.

See the following from the Java Language Spec:
Because Unicode escapes are processed very early, it is not correct to write '\u000a' for a character literal whose value is linefeed (LF); the Unicode escape \u000a is transformed into an actual linefeed in translation step 1 (�3.3) and the linefeed becomes a LineTerminator in step 2 (�3.4), and so the character literal is not valid in step 3. Instead, one should use the escape sequence '\n' (�3.10.6). Similarly, it is not correct to write '\u000d' for a character literal whose value is carriage return (CR). Instead, use '\r'.

[ October 24, 2004: Message edited by: Mike Gershman ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic