File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Please Explain 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 "Please Explain" Watch "Please Explain" New topic

Please Explain

RaviKumar Golagani

Joined: Sep 02, 2003
Posts: 15
public class ADirtyOne
//char a = '\u000A';
Can anyone expalin me, why the above code throws a compile time error even though I commented the line.
It is working finw if I remove the line.
Suresh Thota
Ranch Hand

Joined: Sep 24, 2003
Posts: 152
Hi Ravi,
You might want to see the below discussions.
and lot more if you do a search.

SCJP 1.6
Ritesh Agrawal
Ranch Hand

Joined: Jan 08, 2004
Posts: 74
Hi Ravi,
Try this
Example 1:
class ADirtyOne {
//char a = '\u000A int b = 32;
public static void main(String args[]) {
ADirtyOne ad = new ADirtyOne();
System.out.println("b = " + ad.b);
The output should be
b = 32
Example 2:
Also you can try this statement
char a = '\u000A';
This will compile without any error.
These snippets were just to explain the lexical translation process of the Java Compiler. Read JLS sec 3.2,Sec 3.3, Sec 3.4, and Sec 3.10.4. This will give you a fair idea of how character literals are handled in Java compilation. Actually, \u000A is the Line Feed character in unicode. And during translation phase, the literal \u000A is replaced by Line Feed. This causes the compiler error.
Now, in Example 1 above, code become
// char a = '
int b = 32;
Thus no compiler error. But if I would write,
// char a = '\u000A' int a = 32;
The code would become
// char a= '
' int a = 32;
And thus compiler error.
You can further try this
//char a='\u000Achar d='a';
And try printing d. No compiler error again as code becomes
//char a = '
char d='a';
In the example 2 we thus dont get any error because everything between /* and */ is ignored as shown below
char a = '
(Preparing for SCJP 1.4)
[ March 24, 2004: Message edited by: Ritesh Agrawal ]

Ritesh<br /> <br />SCJP 1.4<br />IBM Test 340<br />IBM AIX V4.0 Certified Professional<br /> <br />Right actions for the future are the best apologies for wrong ones in the past.<br />- Tyron Edwards
I agree. Here's the link:
subject: Please Explain
It's not a secret anymore!