Identifer versus keyword

前端 未结 5 1592
北海茫月
北海茫月 2021-02-16 00:05

I read in the book for OCJP for Java6 the part with assertions. I reached the part where it gives me an overview of how the compiler reacts if the word \'assert\' is used as key

相关标签:
5条回答
  • 2021-02-16 00:15

    I assume an identifier is your own (function name, var name, ...); and a keyword is 'class' or 'assert' or 'while' -- language defined identifiers, in other words

    0 讨论(0)
  • 2021-02-16 00:23

    Identifiers are names of variables. For example in

    int a = 3;
    

    a would the identifier. Keywords, on the other hand, are reserved (i.e. you can't name a variable with a keyword), pre-defined words that have a specific meaning in the language. For example in

    if (a == 3)
        System.out.println("Hello World");
    

    if is a keyword. It has a specific function and cannot be used as a variable name. Moreover, the words used to declare primitive types are all keywords as well, e.g. int, char, long, boolean etc. You can see a full list of Java keywords here

    0 讨论(0)
  • 2021-02-16 00:23

    The following page contains a list of Java identifiers and keywords related to 1Z0-803 OCA Certification. Java Identifiers Keywords

    0 讨论(0)
  • 2021-02-16 00:29

    The terms "keyword" and "identifier" are not Java specific.

    A keyword is a reserved word from the Java keyword list provide the compiler with instructions. As keywords are reserved, they cannot be used by the programmer for variable or method names.

    Examples:

    final
    class
    this
    synchronized
    

    Identifiers are the names of variables, methods, classes, packages and interfaces. They must be composed of letters, numbers, the underscore _ and the dollar sign $. Identifiers may only begin with a letter, the underscore or a dollar sign.

    Examples:

    int index;
    String name;
    

    index and name are valid identifiers here. int is a keyword.

    A keyword cannot be used as an identifier.

    0 讨论(0)
  • 2021-02-16 00:31

    Keywords are reserved words like new,static,public,if,else,..

    An identifier can be a name of any variable.

    int age = 26;
    

    "age" here is an identifier, while int is a reserved word.

    The following example won't compile:

    String static = "hello";
    int public = 4;
    

    you can't do this because "static" and "public" are keywords, that in this case are being used as identifiers, which is not allowed.

    0 讨论(0)
提交回复
热议问题