aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes interesting code 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 "interesting code" Watch "interesting code" New topic
Author

interesting code

challa
Greenhorn

Joined: Mar 28, 2005
Posts: 23
Why the second code compiles and not the first?

public class chartest{
public static void main(String[] args)
{
// char a = '\u000A';
}
}

the above code shows two compile errors
unclosed literal and ";" expected in line 5

however the following code compiles succesfully

public class chartest{
public static void main(String[] args)
{

}
}

can any one explain this ambiguity.
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
Hi purnima

char a = '\u000A';--> is a new line character

while commpiler checks it will display like this

char a ='
';

so it showing error

this is also case with comment line too

only /* */ controls it

i hope now ur clear
challa
Greenhorn

Joined: Mar 28, 2005
Posts: 23
Sagar ,
Thank you for your reply. But i am unable to understand how the compiler checks a comment line.// means entire line is commented ,then how it identifies new line character in that line. Moreover \n ->'\u000a' Are '\u000A''\u000a' both same.
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
'\u000A' is lexically translated before compile-time.

Here is another example with explanation and relevant references.
http://qa.jtiger.org/GetQAndA.action?qids=5&showAnswers=true


Tony Morris
Java Q&A (FAQ, Trivia)
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
is there any way to compile it successfully
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
--------------------------------------------------------------------
Sagar ,
Thank you for your reply. But i am unable to understand how the compiler checks a comment line.// means entire line is commented ,then how it identifies new line character in that line. Moreover \n ->'\u000a' Are '\u000A''\u000a' both same.
-----------------------------------------------------------------------

hi purnima

because in single comment line(//), it checks comment till the end of the line.but when in encounters '\u000a' compiler thinks that line is completed and take rest of the content to next line so it encounters error.it is also same with '\u000d' and '\u000D'

i hope now ur clear
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
is there any way to compile it successfully
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
yes Paramesh...... there is a way

By the use of Multi-Line Comment Statement

/* char ch = '\u000a'; */
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
is there any way to compile it successfully
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
i am sorry. i keep on pressing submit button whenever it says flood control activity.
challa
Greenhorn

Joined: Mar 28, 2005
Posts: 23
Thank you sagar for your clear explanation.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: interesting code