加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java编译器一次不会给出所有错误

发布时间:2020-12-15 00:09:57 所属栏目:Java 来源:网络整理
导读:System.out.println("First eror :: without semicolon ") // first Error System.Out.println("This second error :: OUT object not used proper :: "); //second errorclass TestCompilation{ public static void main(String []args) { System.out.print
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的情况).

如果其中一个步骤失败,则该过程必须停止,因为当代码不符合语法时,编译器无法执行语义分析.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读