Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

interesting code

 
challa
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'\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
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is there any way to compile it successfully
 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
--------------------------------------------------------------------
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
Posts: 485
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is there any way to compile it successfully
 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes Paramesh...... there is a way

By the use of Multi-Line Comment Statement

/* char ch = '\u000a'; */
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is there any way to compile it successfully
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am sorry. i keep on pressing submit button whenever it says flood control activity.
 
challa
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you sagar for your clear explanation.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic