public class Main { public static void main(String[] args) { int i = 012345; System.out.println(i); } } | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

public class Main { public static void main(String[] args) { int i = 012345; System.out.println(i); } }

Why i am getting output result 5349

25th Apr 2019, 6:21 AM
Sonu Kumar
Sonu Kumar - avatar
6 Answers
+ 19
Java recognize number starting with 0 as octal numbers, so converting 012345 to decimal will give 5347, see below the conversion: (0 * 8^5)+(1*8^4)+(2*8^3)+(3*8^2)+(4*8^1)+(5*8^0) =5349 //note: try not to use 0 in beginning of number for showing specific number of digits as it will be taken as octal by many languages(you can use other way if you want to put 0 in beginning, see link to post for that), also you can only use digits 0-7 in octal else it will give error. https://www.sololearn.com/Discuss/1724342/?ref=app
25th Apr 2019, 10:09 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 10
0 at the start, mean the number is in octal (base-8) not decimal (base-10)
25th Apr 2019, 6:31 AM
Taste
Taste - avatar
+ 10
You just remove 0 & the program will be excute.
26th Apr 2019, 10:16 AM
Nasir❤
Nasir❤ - avatar
+ 5
I hope this is not question from challenges, way too much to calculate in such a short time 😐
25th Apr 2019, 10:13 AM
voja
voja - avatar
+ 3
Remove the 0 and try again.
25th Apr 2019, 6:29 AM
Dragonxiv
Dragonxiv - avatar
+ 1
Cause java know every integer which starts by 0 as a octal number not a decimal & (12345)8 to decimal is 5349
26th Apr 2019, 7:50 PM
Abdol Hashimi
Abdol Hashimi - avatar