Java compiler does not give all error at a time

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 } } 

whenever I compile the above code with the JAVAC command in cmd, it gives only the First Error value so as not to give a second error. In the java language, the compiler is partly and the interpreter partly, and in java the first compilation occurs, so the compiler should list all the errors, but this gives me only one error. Why is this happening? I don’t understand, so please help me solve this problem.


I think, now I understand that my question means that the compiler works completely ...

For this I am creating a simple example, this will help you understand that the java compiler is working.

 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 ie 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. :) } } 
+6
source share
3 answers

I would say that this is due to how often compilers work:

  • A lexical analysis is performed , where the source code is converted into a sequence of "tokens".
  • The code is analyzed where the compiler checks if the tokens match the syntax of the language. This means that your first line will not work: every statement in Java must be terminated with a semicolon.
  • A semantic analysis is performed , where the compiler tries to resolve variables, methods, etc. according to the list of known characters - in Java, this roughly translates into your classpath.
  • code is generated where the original operators are translated either into their own bytecode, or into some intermediate bytecode (the latter takes place for Java).

If one of the steps fails, the process should be stopped because the compiler cannot perform semantic analysis when the code does not match the syntax.

+11
source

In the java language, the compiler and partially the interpreter, and in java the first compilation occurs, so the compiler should list all errors

I am sorry to disappoint you, but this is illogical. The interpreter is missing at compile time and has nothing to do with it.

The compiler cannot immediately identify all your errors if, for example, some of them are syntax errors that exclude semantic analysis, or a simple semantic error excludes further analysis.

+3
source

The second error is an error only in the Java program , that is, in the line that corresponds to the Java syntax. The notion of non-syntax error in a non-program does not make sense. For the compiler, all lines that are not syntactically correct have the same value: none.

This is similar to sending the following "program":

 double x = 5.3; x[42] = 0; 

and complain that the compiler is not telling you that a double value cannot be indexed.

For example, entering this input into the java source file in eclipse gives only "Syntax error on tokens, delete these tokens."

+1
source

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


All Articles