Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
313 views
in Technique[技术] by (71.8m points)

java - regarding leading zero in integer value

I have below code

int a = 01111;
System.out.println("output1 = " + a);
System.out.println("output2 = " + Integer.toOctalString(1111));

and output is

output1 = 585
output2 = 2127

I was expecting output to be like below.

output1 = 2127
output2 = 2127

Why does it give 585 when I print direct int value ? I was expecting java to automatically convert value with leading zero to octal.

What is the relation between 01111 and 585?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Leading 0 signifies an octal number (base 8).

01111 (octal) is 1*8^3+1*8^2+1*8^1+1*8^0=585 (decimal)

Integer.toOctalString(1111) converts the decimal number 1111 to an octal String. 2127 octal (2*8^3+1*8^2+2*8^1+7*8^0) is 1111 decimal.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...