为什么java中没有(123 == 0123)? [英] why is not (123 == 0123) in java?

查看:292
本文介绍了为什么java中没有(123 == 0123)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Eclipse在Android中开发应用程序。我编写了以下代码,在测试中,第一个和第三个 if 块无法访问。为什么?

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?

当我向数字添加前导零时,等于运算符返回false。

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
}


推荐答案

0123 是一个八进制数字(前导0 ),而 123 是十进制数。

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

所以0123实际上等于83。

so 0123 actually equals to 83.

这篇关于为什么java中没有(123 == 0123)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆