The mystery of the comment error and other stories ...
Introduction
Do you know the following is a valid Java expression?
\u0069\u006E\u0074 \u0069 \u003D \u0038\u003B
You can try copy and paste it into the main method of any class and compile. If you then add the following statement
System.out.println(i);
and after compilation run this class, the code will print the number 8!
Did you know that this comment throws a syntax error at compile time instead?
/*
* The file will be generated inside the C:\users\claudio folder
*/
However, comments should not lead to syntax errors. In fact, programmers often comment out snippets of code to be ignored by the compiler ... so what's going on?
, , Java char
.
char
, char
Java. . , char
:
char aCharacter = 'a';
, . , , . :
String s = "Java melius semper quam latinam linguam est";
char
, :
(
'&'
).
Unicode (,
'\u0061'
, 97'a'
).
escape- (,
'\n'
).
.
, , char
, , (, «Canc» «Enter» ).
«Esc» — vesper-bot.
, char
, . :
char aUppercase = 'A';
char minus = '-';
char at = '@';
char
2 (16 ), 0 65 535. , «», . ( ) Unicode ( ).
Unicode ( )
, char
16 65 536 . Unicode ( , , . .), . Unicode - , UTF-8, , , 8- ASCII, , , , ASCII code ( American Standard Code for Information Interchange).
Unicode char
, 4 , , \u
( ). :
char phiCharacter = '\u03A6'; // Capital Greek letter Φ
char nonIdentifiedUnicodeCharacter = '\uABC8';
Unicode ( ). , 4 65 536 .
Java 15 Unicode 13.0, , 65 536 . Unicode , 143 859 . . , Unicode, 16- char
, String
Character
, , .
escape-
char
escape-, , :
\b
backspace, ( Delete).
\n
( Ente).
\\
\ ( , \ escape-).
\t
( TAB).
\'
( ).
\"
( ).
\r
( , ).
\f
( , , ).
, '"'
, :
System.out.println('"');
:
char doubleQuotes = '"';
System.out.println(doubleQuotes);
:
"
escape- , , :
System.out.println(''');
, :
error: empty character literal
System.out.println(''');
^
error: unclosed character literal
System.out.println(''');
^
2 errors
, . , :
System.out.println("'IQ'");
:
'IQ'
, \"
escape-, . , :
System.out.println(""IQ"");
:
error: ')' expected
System.out.println(""IQ"");
^
error: ';' expected
System.out.println(""IQ"");
^
2 errors
:
System.out.println("\"IQ\"");
:
"IQ"
Java Unicode
Unicode . , Unicode , . , :
int i = 8;
:
\u0069\u006E\u0074 \u0069 \u003D \u0038\u003B
, :
System.out.println("i = " + i);
:
i = 8
, . , , () .
Unicode escape-
, Unicode , escape-. , , escape- \n
. Unicode 10 ( A). , Unicode:
char lineFeed = '\u000A';
:
error: illegal line end in character literal
char lineFeed = '\u000A';
^
1 error
, :
char lineFeed = '
';
Unicode , Java.
, '
, 39 ( 27) escape- \', Unicode:
char singleQuote = '\u0027';
:
char singleQuote = ''';
:
error: empty character literal
char singleQuote = '\u0027';
^
error: unclosed character literal
char singleQuote = '\u0027';
^
2 errors
, , , .
, D ( 13) escape- \r
. , :
char carriageReturn = '\u000d';
:
error: illegal line end in character literal
char carriageReturn = '\u000d';
^
1 error
, Unicode , , , , .
,
, 92 ( 5C) escape- \
, :
char backSlash = '\u005C';
:
error: unclosed character literal
char backSlash = '\u005C';
^
1 error
, :
char backSlash = '\';
'
escape-, , .
, "
, 22 ( 34) escape- "
, :
char quotationMark = '\u0022';
. :
String quotationMarkString = "\u0022";
:
error: unclosed string literal
String quotationMarkString = "\u0022";
^
1 error
:
String quotationMarkString = """;
Unicode, . , , , !
// char lineFeed = '\u000A';
// char carriageReturn = '\u000d';
, , ; !
, , :
/* char lineFeed = '\u000A';
char carriageReturn = '\u000d'; */
, - , - \u
. , :
/*
* The file will be generated inside the C:\users\claudio folder
*/
4 \u
, :
error: illegal unicode escape * The file will be generated inside the C:\users\claudio folder ^ 1 error
, char
Java . , , Java, Unicode. , Unicode , . , , , .
: 3.3.5 « » 1 «Java ». ( 3.3.5 «»).