Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

integer - why is not (123 == 0123) in java?

I am developing an application in Android using Eclipse. I wrote the following code and in tests the first and third "if" block is not reachable. Why?

When I add a leading zero to a number, the equal operator returns false.

int var = 123;
if (var == 0123) {
    //not reachable
}
if (var == 123) {
    //reachable
}
if (var == (int)0123) {
    //not reachable
}
if (var == (int)123) {
    //reachable
}
See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

0123 is an octal number (leading 0), while 123 is a decimal number.

so 0123 actually equals to 83.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...