What is the output and why? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the output and why?

public class Program { public static void main(String[] args) { int i=010; int j = 07; System.out.println(i); System.out.println(j); } }

25th Sep 2020, 3:23 PM
Gaurav Rawat
Gaurav Rawat - avatar
3 Answers
+ 2
The result is 8 and 7. This is because 010 and 07 are octal representation of integers. In java any number preceded by an 0 is considered to be an octal number
25th Sep 2020, 3:30 PM
Gabriel Ilie
Gabriel Ilie - avatar
+ 3
Gaurav Rawat , variable i is interpreted as octal number, because it starts with 0. When you convert 010 from octal to decimal the result is 8.
25th Sep 2020, 3:31 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 2
Any integer preceded by 0 means it is an octal representation. So decimal representation of 10 is 1×(8^1) + 0×(8^0) 8+0 = 8 Similarly solve for the other.
25th Sep 2020, 3:30 PM
Avinesh
Avinesh - avatar