+ 1

Awkward Output

public class Main { public static void main(String[] args) { int[] x = {120, 200, 016,016}; for(int i = 0; i < x.length; i++){ System.out.print(x[i] + " "); } } }

17th Mar 2022, 6:23 PM
Shubham Gawai
Shubham Gawai - avatar
4 Answers
+ 6
I guess you mean the output of 016. In Java numbers with leading zeros are octal numbers. https://www.tutorialspoint.com/define-integer-literals-as-octal-values-in-java#:~:text=Literals%20with%20a%20leading%20zero,binary%20can%20use%200%2D1.
17th Mar 2022, 6:30 PM
Denise Roßberg
Denise Roßberg - avatar
+ 4
016 => number proceeded or starts with 0 means its an octal number value, in number systems. You will get, Its equalent int in decimal is 14.
17th Mar 2022, 6:32 PM
Jayakrishna 🇼🇳
+ 2
I think the colleagues before explained it pretty well. Consider these prefixes: 0b - binary (e.g. 0b011001) 0x - hexadecimal ( e.g. 0xFF ) 0 - octal ( e.g. your 016) Zero mean that the following ‘16’ is written in octal system. So it’s 6*8^0+1*8^1=8+6=14 in decimal.
17th Mar 2022, 10:21 PM
Nick
+ 1
Output is : 120 200 14 14 i didn't get it, can anyone please explain how ?
17th Mar 2022, 6:27 PM
Shubham Gawai
Shubham Gawai - avatar