How to exit from a program in Java

2 Answers

0 votes
public class MyClass {
    public static void main(String args[]) {
        int x = 34;
        int y = 984;

        System.out.println("exit now");
        System.exit(0);
        
        System.out.println(x + y);
    }
}


/*
run:

exit now

*/

 



answered Mar 21, 2021 by avibootz
0 votes
public class MyClass {
   public static void printNumbers(int[] array) {
        int sum = 0;
       
        for (int number : array) {
            if (number > 50) {
                System.exit(0); // exit the program
            }
             
            System.out.println(number);
        }
    }
 
    public static void main(String args[]) {   
 
        int array[] = {2, 5, 1, 96, 8, 0, 4, 7, 3};
       
        printNumbers(array);
         
        System.out.println("Not Printed");
    }
}
 
 
    
    
    
/*
run:
    
2
5
1
 
*/

 



answered Nov 9, 2023 by avibootz

Related questions

1 answer 195 views
1 answer 216 views
1 answer 232 views
1 answer 88 views
88 views asked Jun 24, 2025 by avibootz
1 answer 99 views
1 answer 94 views
94 views asked Jun 23, 2025 by avibootz
1 answer 91 views
91 views asked Jun 23, 2025 by avibootz
...