Can anyone please give me a good understanding of whats the difference between run-time and compile-time?
相关问题
- Stop child process when parent process stops
- Pygame Distribution - Runtime Error
- Class in jar not found at runtime, but was used to
- Efficiently searching a common node in 2 singly li
- Are objective-c objects all same type of C-structu
相关文章
- Compile-time sizeof conditional
- How to create new type at runtime in F#? [closed]
- How can I create a MetadataWorkspace using metadat
- JNI or Runtime.exec()?
- Create a java class dynamically and compile and in
- Client-side Development Platforms based on JavaScr
- Recursively change system property at runtime in j
- Compile time operators in C
I think of it in terms of errors, and when they can be caught.
Compile time:
A string value can't be assigned a variable of type int, so the compiler knows for sure at compile time that this code has a problem
Run time:
Here the outcome depends on what string was returned by ReadLine(). Some values can be parsed to an int, others can't. This can only be determined at run time
Look into this example:
public class Test {
The above code is compiled successfully,there is no syntax error,it is perfectly valid. But at the run time it throws following error.
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.
Runtime vs Compile time
Runtime and compile time are programming terms that refer to different stages of software program development.
Compile-time is the instance where the code you entered is converted to executable while Run-time is the instance where the executable is running.
The terms "runtime" and "compile time" are often used by programmers to refer to different types of errors too.
Compile-time checking occurs during the compile time. Compile time errors are error occurred due to typing mistake, if we do not follow the proper syntax and semantics of any programming language then compile time errors are thrown by the compiler. They wont let your program to execute a single line until you remove all the syntax errors or until you debug the compile time errors.
The following are usual compile time errors:
Syntax errors
Typechecking errors
Compiler crashes (Rarely)
Run-time type checking happens during run time of programs. Runtime errors are the errors that are generated when the program is in running state. These types of errors will cause your program to behave unexpectedly or may even kill your program. They are often referred as Exceptions . The
following are some usual runtime errors:
Division by zero
Dereferencing a null pointer
Running out of memory
Here is a quote from Daniel Liang, author of 'Introduction to JAVA programming', on the subject of compilation:
...He Continues...
When we punch in high-level/human-readable code this is, at first, useless! It must be translated into a sequence of 'electronic happenings' in your tiny little CPU! The first step towards this is compilation.
Simply put: a compile-time error happens during this phase, while a run-time error occurs later.
Remember: Just because a program is compiled without error does not mean it will run without error.
A Run-time error will occur in the ready, running or waiting part of a programs life-cycle while a compile-time error will occur prior to the 'New' stage of the life cycle.
Example of a Compile-time error:
A Syntax Error - how can your code be compiled into machine level instructions if they are ambiguous?? Your code needs to conform 100% to the syntactical rules of the language otherwise it cannot be compiled into working machine code.
Example of a run-time error:
Running out of memory - A call to a recursive function for example might lead to stack overflow given a variable of a particular degree! How can this be anticipated by the compiler!? it cannot.
And that is the difference between a compile-time error and a run-time error
here's a very simple answer:
Runtime and compile time are programming terms that refer to different stages of software program development. In order to create a program, a developer first writes source code, which defines how the program will function. Small programs may only contain a few hundred lines of source code, while large programs may contain hundreds of thousands of lines of source code. The source code must be compiled into machine code in order to become and executable program. This compilation process is referred to as compile time.(think of a compiler as a translator)
A compiled program can be opened and run by a user. When an application is running, it is called runtime.
The terms "runtime" and "compile time" are often used by programmers to refer to different types of errors. A compile time error is a problem such as a syntax error or missing file reference that prevents the program from successfully compiling. The compiler produces compile time errors and usually indicates what line of the source code is causing the problem.
If a program's source code has already been compiled into an executable program, it may still have bugs that occur while the program is running. Examples include features that don't work, unexpected program behavior, or program crashes. These types of problems are called runtime errors since they occur at runtime.
The reference
As an add-on to the other answers, here's how I'd explain it to a layman:
Your source code is like the blueprint of a ship. It defines how the ship should be made.
If you hand off your blueprint to the shipyard, and they find a defect while building the ship, they'll stop building and report it to you immediately, before the ship has ever left the drydock or touched water. This is a compile-time error. The ship was never even actually floating or using its engines. The error was found because it prevented the ship even being made.
When your code compiles, it's like the ship being completed. Built and ready to go. When you execute your code, that's like launching the ship on a voyage. The passengers are boarded, the engines are running and the hull is on the water, so this is runtime. If your ship has a fatal flaw that sinks it on its maiden voyage (or maybe some voyage after for extra headaches) then it suffered a runtime error.