问题
I have studied about java which have listed 50 Java keywords. There is a homework of Lex, the goal is to recognize the word is keywords, IDs, symbols, operators. But there is one more little problem is the code below, is print
in System.out.print()
an ID or keyword?
public class HelloWorld {
public static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
int c;
int a = 5;
c = add(a, 10);
if (c > 10)
System.out.print("c = " + -c);
else
System.out.print(c);
System.out.print("Hello World");
}
}
回答1:
print is the name of a method in the java.io.PrintStream class, hence an ID. Keywords are those which generally turn blue or another colour when you type them in most IDEs.
For more information: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/index.html
回答2:
System is a final class from java.lang package
.
out is the reference of PrintStream
class and a static member of System
class.
print is a method of PrintStream
class.
//the System class belongs to java.lang package
class System {
public static final PrintStream out;
//...
}
//the Prinstream class belongs to java.io package
class PrintStream{
public void print();
//...
}
Have a look at this too.. https://docs.oracle.com/javase/7/docs/api/java/lang/System.html
来源:https://stackoverflow.com/questions/29823840/in-java-compiler-the-print-in-system-out-print-can-be-defined-as-identifier-or-k