Java编译器一次不给出所有错误
问题内容:
System.out.println("First eror :: without semicolon ") // first Error
System.Out.println("This second error :: OUT object not used proper :: "); //second error
class TestCompilation
{
public static void main(String []args)
{
System.out.println("Hello no semicolon::")//First Error
System.Out.println("Hello out spell not correctely::"); //Second Error
}
}
每当我在cmd中通过JAVAC命令编译以上代码时,它仅给出“第一个错误”即不给出第二个“错误”。在Java语言中,部分编译器和部分解释器以及Java首次编译发生,因此编译器应列出所有错误,但它仅给我一个错误。为什么会发生?我不明白,所以请帮我解决这个问题。
我想我现在很清楚我的问题意味着编译器完全可以工作…
为此,我创建了一个简单的示例,它将帮助您了解Java编译器的工作原理。
class TestCompilation
{
public static void main(String []args)
{
Syste.out.rintln("Hello");//First Error
Syste.Out.rintln("Hello");//Second Error (Not printed at compiler time because it syntatically correct as in compiler first phase)
Hitesh542.add(); //there is no such class called Hitesh542.- but compiler thinks that it is right way to call the method. So it passes the first time.
Hitesh542.add()();//Look out here.. There is a problem, that we can't call a method like this..So it will show the error on first lookup.
System.otu.println("Hello")//second Errorasdasdas
if(); //look this is also an error.- A BASIC syntax error shown at the first lookup.
try{
int i = 10 / 0;
}
//Third error
//So bottom line is the JAVA syntatical errors are checked first i.e The SYNTAX of JAVA not the classes or objects.
//But obv is the first thing is Java Compiler should get the Java class first for compilation on proper path. :)
}
}
问题答案:
我想说这与编译器通常如何工作有关:
- *执行 *词法分析 ,将源代码转换为一系列“令牌”。
- 解析 代码,编译器在其中检查令牌是否符合语言语法。这是您的第一行将失败的地方:Java中的每个语句都必须以分号结尾。
- *执行 *语义分析 ,其中编译器将根据已知符号列表尝试解析变量,方法等-在Java中,这将大致转换为您的类路径。
- *在将源语句转换为本地字节码或某些中间字节码的情况下 *生成代码 (对于Java,后者是这种情况)。
如果其中一个步骤失败,则该过程必须停止,因为当代码不符合语法时,编译器将无法执行语义分析。