AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
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 ?.



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 167
basics x 170
Posted On : 2014-07-30 01:07:42.0
profile Saksham Kumar - anyforum.in Saksham Kumar
72933916025
up-rate
5
down-rate

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 : 3 Yes  0 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523187021394
Reply This Thread
up-rate
5
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in