Runtime vs. Compile time

前端 未结 27 956
后悔当初
后悔当初 2020-11-22 06:50

What is the difference between run-time and compile-time?

相关标签:
27条回答
  • 2020-11-22 07:31

    Imagine that you are a boss and you have an assistant and a maid, and you give them a list of tasks to do, the assistant (compile time) will grab this list and make a checkup to see if the tasks are understandable and that you didn't write in any awkward language or syntax, so he understands that you want to assign someone for a Job so he assign him for you and he understand that you want some coffee, so his role is over and the maid (run time)starts to run those tasks so she goes to make you some coffee but in sudden she doesn’t find any coffee to make so she stops making it or she acts differently and make you some tea (when the program acts differently because he found an error).

    0 讨论(0)
  • 2020-11-22 07:31

    public class RuntimeVsCompileTime {

    public static void main(String[] args) {
    
        //test(new D()); COMPILETIME ERROR
        /**
         * Compiler knows that B is not an instance of A
         */
        test(new B());
    }
    
    /**
     * compiler has no hint whether the actual type is A, B or C
     * C c = (C)a; will be checked during runtime
     * @param a
     */
    public static void test(A a) {
        C c = (C)a;//RUNTIME ERROR
    }
    

    }

    class A{
    

    }

    class B extends A{
    

    }

    class C extends A{
    

    }

    class D{
    

    }

    0 讨论(0)
  • 2020-11-22 07:32

    Compile Time:

    Things that are done at compile time incur (almost) no cost when the resulting program is run, but might incur a large cost when you build the program. Run-Time:

    More or less the exact opposite. Little cost when you build, more cost when the program is run.

    From the other side; If something is done at compile time, it runs only on your machine and if something is run-time, it run on your users machine.

    0 讨论(0)
  • 2020-11-22 07:33

    For example: In a strongly typed language, a type could be checked at compile time or at runtime. At compile time it means, that the compiler complains if the types are not compatible. At runtime means, that you can compile your program just fine but at runtime, it throws an exception.

    0 讨论(0)
  • 2020-11-22 07:37

    Compile Time:

    Things that are done at compile time incur (almost) no cost when the resulting program is run, but might incur a large cost when you build the program.

    Run-Time:

    More or less the exact opposite. Little cost when you build, more cost when the program is run.

    From the other side; If something is done at compile time, it runs only on your machine and if something is run-time, it run on your users machine.

    Relevance

    An example of where this is important would be a unit carrying type. A compile time version (like Boost.Units or my version in D) ends up being just as fast as solving the problem with native floating point code while a run-time version ends up having to pack around information about the units that a value are in and perform checks in them along side every operation. On the other hand, the compile time versions requiter that the units of the values be known at compile time and can't deal with the case where they come from run-time input.

    0 讨论(0)
  • 2020-11-22 07:37

    Look into this example:

    public class Test {
    
        public static void main(String[] args) {
            int[] x=new int[-5];//compile time no error
            System.out.println(x.length);
        }}
    

    The above code is compiled successfully, there is no syntax error, it is perfectly valid. But at the run time, it throws following error.

    Exception in thread "main" java.lang.NegativeArraySizeException
        at Test.main(Test.java:5)
    

    Like when in compile time certain cases has been checked, after that run time certain cases has been checked once the program satisfies all the condition you will get an output. Otherwise, you will get compile time or run time error.

    0 讨论(0)
提交回复
热议问题