Question:
Why does a System.exit(-1) return a 255?
Answer:
When a linux program terminates, a $? from the command-line gives the exit status of the script.
Because in UNIX/POSIX, the exit code of a program is defined to be an unsigned 8-bit value.
Converting -1 to unsigned 8-bit gives 255.
Because in UNIX/POSIX, the exit code of a program is defined to be an unsigned 8-bit value.
Converting -1 to unsigned 8-bit gives 255.
Code:
public class ExitProgram { public static void main(String[] args) { System.out.println("Before exit"); // Terminates the currently running Java Virtual Machine. System.exit(-1); System.out.println("After exit"); } }
Output:
$ java ExitProgram $ echo $? 255