interpretercompiler-theoryjava-compiler-api

Interpreter and Compiler


Can any one help me in figuring out what compilers and interpreters are? And what their difference is ? Appreciate it if explained for Java beginner as I am one.


Solution

  • Basically (very basically), a compiler build your program. It translates your java code into something the computer understands. An interpreter runs your program.

    Both can catch errors but they are different types. Compilation errors can be syntax, semantic or logical errors. On the other hand, errors from your interpreter are only known once you run the program.

    For example, if you have an array that contains 3 fruits like this:

    String[] fruits = ["apple", "banana", "strawberry"]; 
    

    And you try this:

    System.out.println(fruits[4]);
    

    The compiler won't get the error, because there are no syntax errors (everything seems fine at compile-time) but once you run the program, you'll get an IndexOutOfBoundsException which is a RuntimeException caught by the interpreter.