我正在使用IntelliJ IDEA 2018.1.3终极版,需要与大整数(大到不适合
long
,例如
20180531234240565494
)表示为字符串:
public int compareNumeric(String compareTo) {
return new BigInteger(version).compareTo(new BigInteger(compareTo));
}
这是建议的解决方案
here
,我一直认为这是创建
BigInteger
从A
String
.
然而,intellij给出了
the following warning
,通过声纳插件:
构造函数不应用于实例化“string”、“biginger”、“bigdecimal”和基元包装类
鱿鱼:S2129
不应使用字符串、biginger、bigdecimal和用于包装原语的对象的构造函数。这样做比在字符串的情况下简单地使用所需的值,以及在其他任何情况下使用value of更不清晰,占用的内存也更多。
此外,这些构造函数在Java 9中被弃用,这表明它们最终将被完全从语言中删除。
不符合代码示例
String empty = new String(); // Noncompliant; yields essentially "", so just use that.
String nonempty = new String("Hello world"); // Noncompliant
Double myDouble = new Double(1.1); // Noncompliant; use valueOf
Integer integer = new Integer(1); // Noncompliant
Boolean bool = new Boolean(true); // Noncompliant
BigInteger bigInteger = new BigInteger("1"); // Noncompliant
BigDecimal bigDecimal = new BigDecimal(1.1); // Noncompliant<br/>
兼容解决方案
String empty = "";
String nonempty = "Hello world";
Double myDouble = Double.valueOf(1.1);
Integer integer = Integer.valueOf(1);
Boolean bool = Boolean.valueOf(true);
BigInteger bigInteger = BigInteger.valueOf(1);
BigDecimal bigDecimal = BigDecimal.valueOf(1.1);
首先,我不明白
the constructor
在Java 9中被禁止,声纳是错误的吗?
我做的比较是错的并引发了假阳性,还是应该用其他方式进行比较?
我唯一能想到的另一种方法是直接比较字符串,但这也会迫使我首先检查字符串是否是数字。