*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Strange output 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 "Strange output" Watch "Strange output" New topic
Author

Strange output

Udaya Raj
Greenhorn

Joined: Nov 01, 2002
Posts: 26
public class ADirtyOne
{
//char a = '\u000A';
}

Why does the compilation error occurs while compiling this program


IBM Certified WebSphere 4.0 Specialist<br />IBM Certified in OOAD and UML
Netty poestel
Ranch Hand

Joined: Sep 20, 2004
Posts: 131
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

Joined: Sep 28, 2004
Posts: 114
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

Joined: Nov 26, 2003
Posts: 6
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

Joined: Nov 01, 2002
Posts: 26
Any way I have commented it. Why should the compiler care about the line which is commented
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
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 ]

Mike Gershman
SCJP 1.4, SCWCD in process
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strange output
 
Similar Threads
Iimplicit cast
Running code
is khalid wrong?
can we create a class named Object?
Doubt in annotations.