How does this snippet of code prints "-511" as the output on the console?
class Test
{
public static void main(String[] args) {
int i = -0777;
System.out.printf("%d",i);
}
}
Is it to do with the way Java stores Negative numbers?
Integer numbers prefixed with 0
are octal numbers. To use decimal numbers remove the 0
prefix:
int i = -777;
Numbers starting with 0
are treated as being in octal by Java. -077
is equivalent to -63
, which is what I get when I run your program.
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