'\u000a' and '\u000d'

D

dimakura

i found in the web-search why i can not use

////////////////////

char c = '\u000a'

////////////////////

but i can not find why i can not use

////////////////////

// char c = '\u000a'

////////////////////

is it because '\u000a' is equivivalent to \n and this type is comment
is a single-line?

thanks,
dimitri
 
O

Oliver Wong

dimakura said:
i found in the web-search why i can not use

////////////////////

char c = '\u000a'

////////////////////

but i can not find why i can not use

////////////////////

// char c = '\u000a'

////////////////////

is it because '\u000a' is equivivalent to \n and this type is comment
is a single-line?

The process of converting unicode escape sequences to characters happens
somewhere between reading the source file, and then parsing source file for
compilation.

So javac will read in the file, and thus get:

////////////////////
// char c = '\u000a'
////////////////////

Then it will convert unicode escape sequences to their equivalent
characters and get:

////////////////////
// char c = '
'
////////////////////

And then it will try to compile this code, and it'll fail with some sort
of error like "Not expecting apostrophe here".

- Oliver
 
G

Gordon Beaton

is it because '\u000a' is equivivalent to \n and this type is
comment is a single-line?

Yes.

Read section 3.2 of the JLS, which describes translation of the input
to the compiler. The unicode escape sequences are translated into
their corresponding unicode characters, *then* the resulting sequence
of characters is tokenized.

So when you escape a line feed as you've done, you are essentially
writing this (illegal) code:


char c = '
'

i.e. the closing quote ends up on the following line.

Similarly, commenting the line results in this invalid sequence:

// char c = '
'

/gordon
 
C

Chris Uppal

dimakura said:
but i can not find why i can not use
// char c = '\u000a'
is it because '\u000a' is equivivalent to \n and this type is comment
is a single-line?

Yes, exactly right.

-- chris
 
A

Andreas Leitgeb

Chris Uppal said:
Yes, exactly right.

And to test yourself, whether you've really understood,
predict what the compiler will say to that:

// char c = '\u000a//'

// :)
 
D

dimakura

And to test yourself, whether you've really understood,
predict what the compiler will say to that:

// char c = '\u000a//'

// :)


yes, i understand: new line begin with comment!
ok.

just to test myself:

it is not an error:

// \u000a

but error is

// \u000a something_else

where "something_else" is not spaces or something placed in correct
Java-style comment
 
P

Patricia Shanahan

dimakura said:
yes, i understand: new line begin with comment!
ok.

just to test myself:

it is not an error:

// \u000a

but error is

// \u000a something_else

where "something_else" is not spaces or something placed in correct
Java-style comment

It is not an error. It is two lines of code, and something_else is on
the second line, not part of the one line comment. In the following
valid program, ("Hello, world"); is neither spaces nor a Java-style comment.

public class HelloWorld{
public static void main(String[] args){
System.out.println // \u000a ("Hello, world");
}
}

Patricia
 
G

Gordon Beaton

but error is

// \u000a something_else

where "something_else" is not spaces or something placed in correct
Java-style comment

Not just comments and whitespace. It's valid if something_else is
anything that can appear at the start of a line, including statements
or declarations, etc, in the context of the most recent non-comment
before this line, e.g.:

public class
// \u000a Foo {
}

/gordon
 
K

Knute Johnson

Gordon said:
Read section 3.2 of the JLS, which describes translation of the input
to the compiler. The unicode escape sequences are translated into
their corresponding unicode characters, *then* the resulting sequence
of characters is tokenized.

So when you escape a line feed as you've done, you are essentially
writing this (illegal) code:


char c = '
'

i.e. the closing quote ends up on the following line.

Similarly, commenting the line results in this invalid sequence:

// char c = '
'

/gordon

Gordon:

char c = \u0027\u002a\u0027\u003b

Do you know why they would process the unicode prior to determining if
it was part of a comment or literal first? It does provide for some
great obfuscation. I'm really glad it wasn't me that ran across this, I
could have spent days trying to figure this one out :).
 
C

Chris Uppal

Knute said:
char c = \u0027\u002a\u0027\u003b

Do you know why they would process the unicode prior to determining if
it was part of a comment or literal first?

I presume the idea is to allow the use of Unicode characters in identifiers and
comments without making the source completely inaccessible to people using
non-Unicode editors. Also to allow for the case where the source has to be
manipulated by non-Unicode programs (source code control, and so on).

-- chris
 
K

Knute Johnson

Chris said:
I presume the idea is to allow the use of Unicode characters in identifiers and
comments without making the source completely inaccessible to people using
non-Unicode editors. Also to allow for the case where the source has to be
manipulated by non-Unicode programs (source code control, and so on).

-- chris

I guess you have to make the rule one way or the other and this is the
way. It does make for some really interesting traps though.
 
D

dimakura

Not just comments and whitespace. It's valid if something_else is
anything that can appear at the start of a line, including statements
or declarations, etc, in the context of the most recent non-comment
before this line, e.g.:

public class
// \u000a Foo {
}

/gordon


i agree, my formulation was not too precise.
thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top