aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes // commented lines also checked by compiler? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "// commented lines also checked by compiler?" Watch "// commented lines also checked by compiler?" New topic
Author

// commented lines also checked by compiler?

Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
Hi,
This question is from Abihilash's quiz.

even though (1) is commented it gives compiler error. why?
if i change it like this there is no error.

can anyone throw some light?
Thanks,
Vanitha
Stella
Greenhorn

Joined: Jul 02, 2001
Posts: 1
I checked that if the code is changed to :
//char a = 'c' ; //
then no any error. So I think the reason is related to the unicode form of the character.

LOOKING FORWARD IT.<BR>DO IT.<BR>GET IT.<BR>
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
Thanks for your reply.
I understand, this will cause error,
char a = '\u000A';
what is happening when we comment the line that has unicode value?
Vanitha.
wei ma
Ranch Hand

Joined: Jul 18, 2001
Posts: 39

I know it must be some thing with the 'u000A' unicode. Since the unicode is processed very early by the compiler, I guess you code is really
//char a =
; //
because the line feed is interrupted by the compiler before the assignment.
What I am not sure is I typed the code as I thought

and it compiles OK. Anyone has a better idea?
Guoqiao Sun
Ranch Hand

Joined: Jul 18, 2001
Posts: 317
Hi, the code:

compiles ok because <code>;</code> is simply an empty statement.
You can also compile the following without error:

Hope it helps.
Guoqiao

Originally posted by wei ma:
[B]
I know it must be some thing with the 'u000A' unicode. Since the unicode is processed very early by the compiler, I guess you code is really
//char a =
; //
because the line feed is interrupted by the compiler before the assignment.
What I am not sure is I typed the code as I thought

and it compiles OK. Anyone has a better idea?
[/B]


Guoqiao Sun<br />SCJP2 SCWCD2<br />Creator of <a href="http://www.jiris.com/" target="_blank" rel="nofollow">www.jiris.com</a>, Java resource, mock exam, forum
Alokesh Phukan
Greenhorn

Joined: May 24, 2001
Posts: 21
'\u000A' is actually the unicode value for linefeed. And it is an error if either a linefeed or a carriage return('\u000d') appears after the first ' and before a second ' in a char literal.
Please refer to JLS 3.4 & 3.5 for understanding the special treatment by the compiler for unicode escape sequences.
- Alokesh
Originally posted by Vanitha Sugumaran:
Thanks for your reply.
I understand, this will cause error,
char a = '\u000A';
what is happening when we comment the line that has unicode value?
Vanitha.

Eric Pramono
Ranch Hand

Joined: Jul 09, 2001
Posts: 74
Hi all,
I think that the code will look like this:
public class ADirtyOne {
//char a = '
'; //(1)
}
and that's what makes the compiler error.
please correct me if I'm wrong.
- eric
Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
Posts: 2370

The following also works fine!

No problem with the /*...*/ or /** ... */ comments.
Only // creates problem for '\u000A'.....;(


------------------
azaman


Ashik Uzzaman
Senior Member of Technical Staff, Salesforce.com, San Francisco, CA, USA.
Eric Pramono
Ranch Hand

Joined: Jul 09, 2001
Posts: 74
Hi Azaman,
I think that's because /*..*/ doesn't care about LF in it..
but // will only comment out the line following it, so an LF would totally screw it up.
- eric
Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
Posts: 2370

Never mind Eric,
What's LF?

------------------
azaman
Eric Pramono
Ranch Hand

Joined: Jul 09, 2001
Posts: 74
Hi Azaman,
LF = line feed
CR = carriage return
it's one of those character inserted after each lines.. so that it'll start on the next line.
- eric
Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
Posts: 2370

Thnx Eric,
Line Feed (LF) is "\n" and prints a newline character to take the cursor to the next line and Carrige ruturn (CR) is to take the focus on the first character of the line overriding others if any. Right?


------------------
azaman
Eric Pramono
Ranch Hand

Joined: Jul 09, 2001
Posts: 74
Yup.
that's exactly what I'm trying to say.
- eric
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
Thanks all for your reply. Eric, so the compiler will read the unicode values first, right?
//char a = '\u000B;
This works fine.
Vanitha.
Samith Nambiar
Ranch Hand

Joined: Mar 14, 2001
Posts: 147
hi here are my thoughts on this topic
1. //char a = '\u000A'; //
this line gives a compiler error inspite of being commented because unicode escapes are processed very early during the compilation process .... and so \u000A is treated as a LF which totally distorts the line.
2. /*char a = '\u000A'; */
this type of comment does not give an error because it is a multiline comment and does accept a line feed inside the comment
hope that helps
Samith.P.Nambiar
<pre>
\```/
(o o) harder u try luckier u get
-------oOO--(_)--OOo----------------------------
</pre>
wei ma
Ranch Hand

Joined: Jul 18, 2001
Posts: 39
Thanks eric, I finally understand now, I guess this is the same deal for '\u000d' ?
Wei

Hi all,
I think that the code will look like this:
public class ADirtyOne {
//char a = '
'; //(1)
}
and that's what makes the compiler error.
please correct me if I'm wrong.
- eric
Eric Pramono
Ranch Hand

Joined: Jul 09, 2001
Posts: 74
To Vanitha:
Yes, I believe that's correct. The compiler will process all Unicode values first, even before checking the Java syntax.
To Wei Ma:
Here's an example for carriage return

please not that in the 3rd line, the '\u000d' (CR) will reset the line, and therefore will erase the comment-mark, and become a simple statement:
int i=1;
in the 4th line, again CR will reset the comment-mark, and become a simple statement:
i=9;
hope this helps..
- eric
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
Thanks Eric for your explanation.
Vanitha.
Rashmi Hosalli
Ranch Hand

Joined: Jun 25, 2001
Posts: 50
Hi Eric,
your code:
public class X
{
public static void main (String[] args)
{
// char a='\u000d' int i=1;
// \u000d i=9;
System.out.println(i);
}
}
Does not compile.I get the following
1. unclosed character literal on line 6 i.e \u000d i=9;
2. cannot resolve symbol for variable i.
I know that 2 is obvious as 1 did not go thro'...
Can you tell me what's happening here?
Thanks,
Rashmi
Eric Pramono
Ranch Hand

Joined: Jul 09, 2001
Posts: 74
Hi Rashmi,
Thanks for pointing that out.
It's interesting to note that my previous code compiles well in Jdk1.2.2., but when I try and compile it in Jdk1.3., it gives the compiler error as you mentioned.
So, I try the following code (removing the closing ' from variable a declaration):

This time, it compiles and gives the same result (9) in both Jdk1.2.2. and Jdk1.3. My assumption is Jdk1.2.2 has a bug in its compiler, either:
1) ignoring the closing ', which in turn will act like this:
' int i=1;
and, allowed it.
Note: I've tried to declare a variable with ' in the beginning of the line and it didn't compile (of course ), so this is not the bug with compiler.
2) or.. it will regard either '\u000d' or '\u000d as the same CR character, and will result in:
int i=1;
Note: I have even tried using \u000d' (without the beginning '), it didn't compile in Jdk1.3., but it gives me (9) in result, while I'm using Jdk1.2.2.
So, my assumption is that Jdk1.2.2. compiler sees: '\u000d', '\u000d, \u000d' as the same.
please correct me if I'm wrong.
- eric
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
Hi Eric, I am using jdk/1.2.2 it is working without any ''.


Vanitha.
Rashmi Hosalli
Ranch Hand

Joined: Jun 25, 2001
Posts: 50
Thanks Eric...it did work.I think you are right.
Rashmi
Eric Pramono
Ranch Hand

Joined: Jul 09, 2001
Posts: 74
Hi Vanitha,
yes, I believe if you try without ' ' then it'll gives the same result, because it just clears the line and start with the next sequence of character in the line to be parsed.
- eric
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: // commented lines also checked by compiler?