System.exit and return.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    System.exit and return.

    Have a look at my code.

    Code:
    clas MainClass{
    public static void main(String a[]){
     int a = 10;
     if(a==10) System.exit(0);
    }
    }
    2nd version....

    Code:
    clas MainClass{
    public static void main(String a[]){
     int a = 10;
     if(a==10) return;
    }
    }
    When you run a class file using JVM then JVM starts up and automatically shuts down when main method terminates. Here in both cases at line 4 the main method terminates. So here return & System.exit doing same thing?
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    return stops the execution of the current method. If that method is the main method then the program exits. I would have expected that to be rather obvious to you.

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      Ah .............. Actually only the main method stuck to my mind. I didn't consider the usage of return in another methods;)

      Comment

      Working...