octal literals八进制:java中0开头的数据被当成八进制
decimal literals 十进制
发生数据进制问题导致的数据错误问题:
IIDEA在数字上报黄色警告如下 nspection info: Reports octal integer literals. Some coding standards prohibit the use of octal literals, as they may be easily confused with decimal literals.
即以0开头的数据会被当作八进制,所以011存进去就变成了9:
public static void main(String[] args) {
HashMap<String, Object> map = new HashMap<>();
map.put("num", 011);
System.out.println("八进制:"+map.get("num"));
}
结果:
因此0后面跟大于7的数字会报错(八进制数据范围只有0到7):
所以,数值最好别写0开头,一定要存01、02的可以用字符串类型存储,避免数据因为进制问题产生错误。
public static void main(String[] args) {
HashMap<String, Object> map = new HashMap<>();
map.put("num", "012");
System.out.println("字符串:"+map.get("num"));
}
PS:以0x开头的数值是16进制,数据范围为0到f(0-9,a-f)。