Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Comment Statement

 
usha prithvi
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

this is question 28 from Abhilash java quiz

public class ADirtyOne
{
//char a = '\u000A';
}
An attempt to compile the above class
1.will complete successfully.
2.will compile sucessfully but with a warning message.
3.will not compile - complains on an invalid expression.

I think the answer is 1.But the given answer is 3. I tried to compile the code, and itz showing the error.When // used for commenting, it comments the entire statement next to it right? Then why is the compiler showing the errror?

Thankz for the reply.
 
Mahesh Kumar Kumar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you notice the unicode character, I believe it is LF, hence it interprets it as:
//char a = '
';

Therefore the error
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try our SCJP FAQ
 
usha prithvi
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Barry,

From the link provided I understood that char A = '\u000A'; gives compiler error. But in the code written //char A = '\u000A; the line should be ignored since // is present. Still, why does the compiler shows an error? PLease explain.

Thankz
[ July 14, 2005: Message edited by: usha prithvi ]
 
bhavesh bhanushali
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class ADirtyOne
{
//char a = '\u000A';
}

what happens is that the unicode character '\u000A' is for a new line.
So when the compiler compiles the line it takes the character to the new line which is uncommented , because of which we get the compile time error

others please clarify

regards ,
bhavesh
 
Joe Sondow
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unicode escape sequences such as \u000A are parsed by the compiler and converted to their Unicode character equivalents before the source code is parsed for comments. So by the time the compiler gets around to evaluating which lines are commented out, the code has already been interpreted as

 
Tony Morris
Ranch Hand
Posts: 1608
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic