In Java if I print (019) , why I get a error. ? | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
+ 1

In Java if I print (019) , why I get a error. ?

System.out.print(019) ; This code gives a runtime error that " integer no. is too large", why is it so? This even is not consistent as I am also getting outputs "10" if I print(012) and "18" if I try to print(022); Someone please explain how this outputs​ are coming.

29th Nov 2017, 1:56 AM
Mr.Curious
Mr.Curious - avatar
3 Respuestas
+ 3
Yes, a hex literal can be prefixed with 0x and a binary literal can be prefixed with 0b https://docs.oracle.com/javase/8/docs/technotes/guides/language/binary-literals.html https://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html Scroll down to 3.10.1. Integer Literals
29th Nov 2017, 2:33 AM
ChaoticDawg
ChaoticDawg - avatar
+ 5
When you prefix the number with 0 like this, it indicates that you are using an octal literal (base 8) and not a typical decimal (base 10) number. Then 9 is in the ones place which is to large since an octal would go from 0-7 in the ones place (or any other place really). 010 octal would be equal to 8 in decimal. 012 octal the 0 indicates you are using an Octal. 1 is in the eights place and means 1 eight is added to the number. 2 is in the ones place which means add 2 ones to the number 8+2 = 10 022 means 2 eights and 2 ones 8+8+2 = 18
29th Nov 2017, 2:20 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
Thanks a lot , Is there any other similar convention for writing hexadecimal & binary.
29th Nov 2017, 2:24 AM
Mr.Curious
Mr.Curious - avatar