简体   繁体   中英

In Java compiler,which type can be defined as identifier ( ID ) or Keyword (reserved word)?

I have a simple question:
In Java Compiler, Which type of method or variables can be defined as identifier (ID) or Keyword (reserved word)?

For following example, ID should be : add , main , a , b , c , Test1 , What about print , is print an ID or keyword?

Example:

public class Test1 {
    public static int add(int a, int b) {
        return a + b;
    }
    public static void main() {
        int c;
        int a = 5;
        c = add(a, 10);
        if (c > 10)
            print("c = " + -c);
        else
            print(c);
        print("Hello World");
    }
}

The Java keywords are part of the language, and are documented in the Java Language . You cannot use a keyword as an identifier . const and goto are reserved keywords, but not implemented. true , false , and null are literals; you still cannot use them as identifiers but they are not keywords.

From the linked Java tutorial, the keywords are

  • abstract
  • continue
  • for
  • new
  • switch
  • assert 3
  • default
  • goto 1
  • package
  • synchronized
  • boolean
  • do
  • if
  • private
  • this
  • break
  • double
  • implements
  • protected
  • throw
  • byte
  • else
  • import
  • public
  • throws
  • case
  • enum 4
  • instanceof
  • return
  • transient
  • catch
  • extends
  • int
  • short
  • try
  • char
  • final
  • interface
  • static
  • void
  • class
  • finally
  • long
  • strictfp 2
  • volatile
  • const 1
  • float
  • native
  • super
  • while

1 not used

2 added in 1.2

3 added in 1.4

4 added in 5.0

An identifier is a word used by a programmer to name a variable, method, class, or label .

        // Test1 is a class name identifier 
        public class Test1 {
                public static int add(int a, int b) { // add is identifier for a method
                      return a + b; 
                 }  

                public static void main() {
                    int c; // c is identifier for a variable
                    int a = 5;
                    c = add(a, 10);
                    if (c > 10)
                         print("c = " + -c);
                    else
                        print(c);
                    print("Hello World");
                 } 
        }

You cannot use any of the Keywords as identifiers in your java programs

print in your above program is not a Keyword , you can use print as a identifier

After using print as a identifier your code looks like this.

//Test1 is a class name identifier 
public class Test1 {
    // add is identifier for a method
    public static int add(int a, int b) {
    return a + b;
}

public static void main(String[] args) {
    int c; // c is identifier for a variable
    int a = 5;
    c = add(a, 10);
    if (c > 10)
        print("c = " + -c); // c is a String
    else
        print(c); // c is a int
    print("Hello World"); // Hello World is a String
}

/**
 * Method Overriding
 */
private static void print(int c) {
    System.out.println("In Integer Print Method "+c);
}

private static void print(String string) {
    System.out.println("In String Print Method "+string);
}

}

Also Refer :

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM