What is the reason behind error in this code ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 10

What is the reason behind error in this code ?

int a=028; System.out.println(a);

1st Jul 2018, 7:22 AM
Nikhil Garg
Nikhil Garg - avatar
9 Answers
+ 11
when you add 0 to the beginning it is considered as octal digit (base 8) so, it displayed value too large Ref: https://stackoverflow.com/questions/6935345/why-is-09-too-large-of-an-integer-number
1st Jul 2018, 7:41 AM
‎ ‏‏‎Anonymous Guy
+ 10
The presence of 0 as the very first digit of the number 028 makes JVM consider it as an Octal number with base 8. Since octal numbers can never contain a digit greater than 7, it gives an error. 028 is not fundamentally an octal number!
1st Jul 2018, 7:45 AM
Dev
Dev - avatar
+ 7
thank you, for posting this question.
1st Jul 2018, 12:12 PM
Tarika
Tarika - avatar
+ 5
it's because 8 doesn't exist in base8
1st Jul 2018, 3:59 PM
Pls Dont Block Me
+ 3
remove the zero
2nd Jul 2018, 8:26 AM
Wajeeh
Wajeeh - avatar
+ 2
thanks dev and sreejith these explanations are very helpful
1st Jul 2018, 7:53 AM
Nikhil Garg
Nikhil Garg - avatar
+ 1
System.out.print(a);
1st Jul 2018, 7:34 AM
‎ ‏‏‎Anonymous Guy
0
whenever you start assigning integer from 0 then JVM assumes that it is as an Octal value. as you here defined here as integer but JVM will interpret as Octal so it will throw error.
2nd Jul 2018, 10:15 AM
Mahamadayaj Salim Mhaldar
Mahamadayaj Salim Mhaldar - avatar
0
You cant have the 0 at the beggining
3rd Jul 2018, 8:17 AM
n3m0
n3m0 - avatar