AF
Home
Tag
Submit Notes
Ask Anything
Login
Subscribe Us
A
ny
F
orum
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.
Follow @anyforumin
programming-basics: why int=012 works like 10?
public class Literal{
public static void main(String[] args) {
int a=012;
int c=a*2;
System.out.print(c);
}
}
output:
20
Please describe that how above program printing 20 on console.
programming x 169
basics x 171
Posted On :
2014-07-30 01:07:42.0
Saksham Kumar
734
339
39909
5
Answers
It is simple, If you put integer value starting with 0(zero), It´s taken as octal base(8). e.g. If you change the value of a to 0124 and print the value of a on console, It will print 84. See the below explanation:
int a=012--> 2*8^0 + 1*8^1 --> 2+8 -->10
int a=0124--> 4*8^0 + 2*8^1 + 1*8^2--> 4+16+64 -->84
So i hope you now got the point why your program printing 20.
Posted On :
2014-07-30 01:21:55
Satisfied :
4 Yes
0 No
Rishi Kumar
523
1882
50047
Reply This Thread
5
Post Answer
Please Login First to Post Answer:
Login
Answer:
anyforum.in