Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

// commented lines also checked by compiler?

 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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]

 
Alokesh Phukan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'\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
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2373
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following also works fine!

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


------------------
azaman
 
Eric Pramono
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2373
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never mind Eric,
What's LF?

------------------
azaman
 
Eric Pramono
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2373
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup.
that's exactly what I'm trying to say.
- eric
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Eric for your explanation.
Vanitha.
 
Rashmi Hosalli
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Eric, I am using jdk/1.2.2 it is working without any ''.


Vanitha.
 
Rashmi Hosalli
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Eric...it did work.I think you are right.
Rashmi
 
Eric Pramono
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic