difference between System.exit(0) and System.exit(-1)

ramana picture ramana · Aug 4, 2011 · Viewed 28.4k times · Source

Can any one share to me difference between System.exit(0) and System.exit(-1) it is helpful if you explain with example.

Answer

Jon Skeet picture Jon Skeet · Aug 4, 2011

It's just the difference in terms of the exit code of the process. So if anything was going to take action based on the exit code (where 0 is typically success, and non-zero usually indicates an error) you can control what they see.

As an example, take this tiny Java program, which uses the number of command line arguments as the exit code:

public class Test {    
    public static void main(String[] args) throws Exception {
        System.exit(args.length);
    }
}

Now running it from a bash shell, where && means "execute the second command if the first one is successful" we can have:

~ $ java Test && echo Success!
Success!

~ $ java Test boom && echo Success!