长值,左边为0 [英] long value with 0 on left

查看:105
本文介绍了长值,左边为0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么会出现这种情况?

Why this behavior happens?

long value = 123450;
System.out.println("value: " + value); 

价值:123450

 

long value = 0123450;
//           ^
System.out.println("value: " + value); 

价值:42792

这是什么42792?

What is this 42792?

推荐答案


为什么会出现这种情况?

正如以 0x 开头的文字被视为十六进制数字(基数为16),文字以a开头 0 被视为 八进制数 ,即基数为8的数字。

Just as literals starting with 0x are treated as hexadecimal numbers (base 16), literals starting with a 0 are treated as octal numbers, i.e., numbers in base 8.

(尝试编写0789,你会看到编译器会抱怨。)

(Try writing 0789, and you'll see that the compiler will complain.)


这是什么42792?

基数8中的数字123450代表数字

The number 123450 in base 8 represents the number

1 &次; 8 5 + 2 × 8 4 + 3 × 8 3 + 4 × 8 2 + 5 × 8 1 + 0 × 8 0 = 42792

1×85 + 2×84 + 3×83 + 4×82 + 5×81 + 0×80 = 42792

这篇关于长值,左边为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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