my dog learned polymorphism
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes character literals 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 "character literals" Watch "character literals" New topic

character literals

natarajan meghanathan
Ranch Hand

Joined: Feb 01, 2001
Posts: 130
What is the problem with the following simple code. I get compile time error:
public class ADirtyOne
char a = '\u000a';

char a = '\u007a'; compiles properly.

Sun Certified Programmer for Java 2 Platform
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
This has been discussed before, so if you need more explanation, do a search for 'u\000a'.
Basically it is because what does 'u\000a' represent? It is the new line feed. So when the compiler goes through the code, the first thing it does is replace all the unicode with the literal meaning, so you replace 'u\000a' with a line feed. Then you get:
char a =
which won't compile.
Anshuman Acharya
Ranch Hand

Joined: Jan 19, 2001
Posts: 144
the same goes for char = '\u000d'
Tualha Khan
Ranch Hand

Joined: Nov 22, 2000
Posts: 287
I read in the JLS, that the unicode escape sequence based char literals are processed at the earliest. So that's why '\u000a' is processed immediately and the ; in the end is taken to the next line.
Tualha Khan

SCJP2, BEA WLS 6.0, DB2 UDB 7.1
I agree. Here's the link:
subject: character literals
It's not a secret anymore!