File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Comment Statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Comment Statement" Watch "Comment Statement" New topic
Author

Comment Statement

usha prithvi
Ranch Hand

Joined: Jun 22, 2005
Posts: 31
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

Joined: Jul 14, 2005
Posts: 8
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

Joined: Aug 03, 2002
Posts: 7729
Try our SCJP FAQ


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
usha prithvi
Ranch Hand

Joined: Jun 22, 2005
Posts: 31
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

Joined: Jun 13, 2005
Posts: 55
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

Joined: Apr 10, 2005
Posts: 195
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



SCJA 1.0 (98%), SCJP 1.4 (98%)
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
On a related note with attached explanation:
http://qa.jtiger.org/GetQAndA.action?qids=5
and
http://qa.jtiger.org/GetQAndA.action?qids=8


Tony Morris
Java Q&A (FAQ, Trivia)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Comment Statement
 
Similar Threads
Access modifiers
Eduardo Cobian's mock question 4
Question on Dan's Mock
Need help in casting topic
Package visibility