gpt4 book ai didi

java - Android 和 Java 短裤

转载 作者:太空狗 更新时间:2023-10-29 15:49:23 25 4
gpt4 key购买 nike

我写了一些使用字符串表示时间的代码,例如“0620”,但经过仔细思考后我意识到这些可以解析为短裤以比较性能和存储 yield 。

在单元测试中,我将一个短写为 0620 的值与返回值 620 进行比较,希望它能正常工作。但是,测试 0620 等于 620(两者都是空头)失败了。

错误消息说 0620 被中断为 400(并且 400 != 620 解释了失败)。删除 lead 0 可以解决问题,但我想知道这是怎么回事?

这行不通

assertEquals("Time Failed", 0620, st.GetTime());

这行得通

assertEquals("Time Failed", 620, st.GetTime());

最佳答案

根据 Java language specification,以 0 开头的整数文字被解释为八进制(基数 8) :

An octal numeral consists of an ASCII digit 0 followed by one or more of the ASCII digits 0 through 7 and can represent a positive, zero, or negative integer.

我相信这是 Java 从 C 继承而来的怪癖之一。

关于java - Android 和 Java 短裤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4636201/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com