All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To End A Program In Java

Last Updated : Mar 11, 2024

How To End A Program In Java

In this article we will show you the solution of how to end a program in java, the programming language Java is very popular. It may execute any Java program on the Java Virtual Machine (JVM). JVM will terminate the program as soon as it is stopped.

Programs in Java usually terminate when they reach the end of their execution; however, there are some circumstances where they may need to be terminated while executing a condition.

Using Java's methods, you can end that particular program in such circumstances.

Java virtual machines running on a system are terminated by the exit() method of the System class.

You can enter the status code using this way. We will now discuss the idea of how to end a program in java with an example.

Step By Step Guide On How To End A Program In Java :-

package com.talkerscode;
import java.lang.*;
public class TalkersCode{
   public static void main(String[] args) {
      int arr1[] = { 0, 1, 2, 3, 4, 5 };
      int arr2[] = { 0, 10, 20, 30, 40, 50 };
      int i;
      // copies an array from the specified source array
      System.arraycopy(arr1, 0, arr2, 0, 1);
      System.out.print("array2 = ");
      System.out.print(arr2[0] + " ");
      System.out.print(arr2[1] + " ");
         System.out.println(arr2[2] + " ");
      for(i = 0;i < 3;i++) {
         if(arr2[i] > = 20) {
            System.out.println("exit...");
            System.exit(0);
         } else {
            System.out.println("arr2["+i+"] = " + arr2[i]);
         }
      }
   }
}
  1. Class TalkersCode is defined at the beginning of the code.
  2. Within the class which acts as the program's entry point, the main method is defined.
  3. Two integer arrays are initialized with values of size 6, both containing values from 0 to 5 and 0 to 50 respectively.
  4. An integer variable 'i' is declared, which will be used in a loop to iterate over the elements in one of the arrays later in the program.
  5. First elements of both arrays are copied using the System.arraycopy() function.
  6. The information in the second array must then be printed to the console.
  7. In the second array, the first three elements are iterated over by means of a for loop.
  8. Each element is checked if it exceeds or equals 20 using an if statement. In the event that an element exceeds 20 or is equal to 20, then the program will exit.
  9. Scripts that print element indexes and values will be displayed if that element is less than 20.
  10. Program termination will follow the completion of the loop.

Conclusion :-

As a result, we have successfully learned how to end a program in java with example.

Java programs can be terminated with the System.exit() method or a return statement.

There are two ways to handle this scenario, but the most commonly used method is System.exit().

As a result of the System.exit() method, the JVM currently running is terminated, and the return statement ends the execution of a function or returns the value.

I hope this article on how to end a program in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪