Java definitions: shortcut, token

I wrote this:

(fitness>g.fitness) ? return 1 : return -1;

and got the following error:

Syntax error on tokens, a label is expected instead.

Can someone explain what tokens and labels mean in this context?

Edit: thanks for fixing my code, but could you please explain what markers and shortcuts are for the future link?

+3
source share
4 answers

Tokens are separate characters and strings that have some kind of meaning.

Tokens as defined in Chapter 3: The Lexical Structure of Java Language Specification :

(§3.8), keywords (§3.9), literals (§3.10), delimiters (§3.11), and operators (§3.12) grammar syntax.

Tokens in this line:

"(", "fitness", ">", "g.fitness", ")", "?", "return", "1", ":", "return", "-1", ";"

( , .)


Java , .

hello:.

continue break, , continue break .

Labeled Statement 14.7 Java .


return:

(fitness>g.fitness) ? return 1 : return -1;
                      ^^^^^^

return 1 :, , .

return 1 , , , , .

(fitness>g.fitness) ?  return 1   :   return -1;
                       ^^^^^^^^   ^
                      statement   label without an identifier
+6

- ?: , .

return (fitness > g.fitness) ? 1 : -1;

, , .

, , . , , .

, , . .

+5

:

return (fitness>g.fitness) ? 1 : -1;
+1

I think the compiler tells you that since there is a colon in the code, it thinks you are trying to declare a statement , but it cannot parse it because your syntax is incorrect.

0
source

Source: https://habr.com/ru/post/1727023/


All Articles