• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please Explain

 
RaviKumar Golagani
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class ADirtyOne
{
//char a = '\u000A';
}
Hi,
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.
Cheers
Ravi
 
Suresh Thota
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ravi,
You might want to see the below discussions.
http://www.javaranch.com/certfaq.jsp#q14
http://www.coderanch.com/t/244833/java-programmer-SCJP/certification/Valid-char-values
and lot more if you do a search.
Cheers
-Suresh
 
Ritesh Agrawal
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 = '
';
*/
Regards
Ritesh
(Preparing for SCJP 1.4)
[ March 24, 2004: Message edited by: Ritesh Agrawal ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic