The java compiler does not give all errors at once

System.out.println("First eror ::  without semicolon ") // first Error
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 through the javac command in CMD, it only gives the first error, which means that it does not give the second error In the Java language, some compilers and some interpreters compile first in Java, so the compiler should list all errors, but it only gives me one error Why is that? I don't understand, so please help me solve this problem

I think now I know that my problem means that the compiler works completely

I created a simple example to help you understand how the java compiler works

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. :)
    }
}

Solution

I would say that this has something to do with how often compilers work:

>Perform lexical analysis and convert the source code into "token" sequence. > Parse the code, and the compiler checks whether the token conforms to the language syntax This is where your first line fails: every statement in Java must end with a semicolon. > Perform semantic analysis, and the compiler attempts to parse variables, methods, etc. according to the list of known symbols - in Java, this is roughly converted to a class path. > Generate code where the source statement is converted to native bytecode or some intermediate bytecode (the latter is the case in Java)

If one of the steps fails, the process must stop because the compiler cannot perform semantic analysis when the code does not conform to syntax

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>