JAVA
What is Runtime?
Definition
Runtime is a system used primarily in software development to describe the period of time during which a program is running.
Overview
Runtime is the final phase of the program lifecycle in which the machine executes the program’s code.
The other phases include:
- Edit time – When the source code of the program is being edited. This phase includes bug fixing, refactoring, and adding new features.
- Compile time – When the source code is translated into machine code by a compiler. The result is an executable.
- Link time – When all the necessary machine code components of a program are connected such as external libraries. These connections can be made by the compiler (called static linking) or by the operating system (called dynamic linking).
- Distribution time – When a program is transferred to a user as an executable or source code. Most of the time a program is downloaded from the Internet but it can also be distributed via CD or USB drive.
- Installation time – When the distributed program is being installed on the user’s computer.
- Load time – When the operating system places the executable in active memory in order to begin execution.
How Runtime Works
When a user tries to start a program a loader runs that allocates memory and links the program with any necessary libraries, then the execution begins. Many people who use computer programs understand the runtime process; however, runtime is very important to software developers because if errors are found in the code the program will throw runtime errors.
Runtime errors
If a program experiences an error after it has been executed it will report back a runtime error. There are hundreds of different errors that programs can experience such as division by zero errors, domain errors, and arithmetic underflow errors.
Some programming languages have built-in exception handling which is designed to handle any runtime errors the code encounters. Exception handling can catch both predictable and unpredictable errors without excessive inline, manual error checking.
Taking Java as an example, there are multiple ways to implement exception handling. Below we will cover try-catch blocks and throws.
The following type of exception handling is called a try-catch block. It tells the program to try a block of code and, if it doesn’t work, catch the exception and run another block of code:
public static String readFirstLine(String url) {
try {
Scanner scanner = new Scanner(new File(url));
return scanner.nextLine();
} catch(FileNotFoundException ex) {
System.out.println("File not found.");
return null;
}
}
The next type of exception handling is called a throw. It tells the program to explicitly throw an exception object if specific criteria are met:
public class ThrowExample {
static void checkEligibilty(int stuage, int stuweight){
if(stuage<12 && stuweight<40) {
throw new ArithmeticException("Student is not eligible for registration");
} else {
System.out.println("Student Entry is Valid!!");
}
}
public static void main(String args[]){
System.out.println("Welcome to the Registration process!!");
checkEligibilty(10, 39);
System.out.println("Have a nice day..");
}
}
//If the student does not meet the necessary criteria,
//we will encounter the following error message.
Welcome to the Registration process!!Exception in thread "main"
java.lang.ArithmeticException: Student is not eligible for registration
Examples of Runtime
Developers can manipulate and send instructions to a program while testing their program in a runtime environment (RTE). One of the most popular runtime environments for JavaScript is Node.js. JavaScript is primarily a client-side language but this open-source runtime environment allows for the creation of web servers and networking tools using JavaScript. Many major corporations implement Node.js in their development such as Netflix, LinkedIn, Walmart, Uber, eBay, and PayPal.
Another runtime environment that is popularly used is Android Runtime (ART). As expected, it is used by the Android operating system to perform the translation of bytecode into native instructions. ART uses a hybrid combination of ahead-of-time (AOT) compilation, just-in-time (JIT) compilation, and profile-guided compilation to drastically increase app performance.
Key Takeaways
- Runtime is the phase of the program lifecycle that executes and keeps a program running; other phases include edit time, compile time, link time, distribution time, installation time, and load time.
- Developers often test their programs in runtime environments (RTE) before moving to production in order to check for performance glitches and runtime errors.
- Runtime errors can be customized in most languages; in JavaScript, a custom message can be displayed with a standard error block by using throw.
Comments
Post a Comment