System.out.println(Integer.parseInt(e.getMessage()));
System.out.println(e.getMessage());
System.exit(Integer.parseInt(e.getMessage()));
when I am runnig the code in unix system.exit(Integer.parseInt(e.getMessage()))
is giving 254
output:
-2
-2
254
Your operating system's exit codes are unsigned 8-bit integers, so the only valid exit codes are 0..255.
The reason you are getting 254 is because it's the lower 8 bits of int
-2 treated as an unsigned number.
$ for q in -2 -1 0 1 2 253 254 255 256 257 ; do
perl -e'exit $ARGV[0]' -- $q
printf '%3d => %3d\n' $q $?
done
-2 => 254
-1 => 255
0 => 0
1 => 1
2 => 2
253 => 253
254 => 254
255 => 255
256 => 0
257 => 1
Since System.exit() needs to take a positive value between 0-255 the -2
will become 256 - 2 = 254
.
The exit status code on linux and Unix is 8 bits and unsigned (0-255).
A binary level conversion occurs from int
to byte
here (254 is the low level byte of the -2 integer).
Note that this doesn't happen in java (the native Shutdown.halt0
method receives an int) as this could be different on a different platform.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With