弃用的Short(String)如何更新?

我正在处理这个问题:

produto.setQuantidade(new Short("7"));

Short显示已弃用,并带有标记。如何为以上 Java 9 版本更新此内容?(现在,我有 Java 11)

回答

您将使用Short.valueOf. 这是取自javadoc

已弃用。很少适合使用此构造函数。静态工厂 valueOf(short) 通常是更好的选择,因为它可能会产生明显更好的空间和时间性能

注意:公共构造函数forRemoval从 Java-16 开始。

  • @Michael these constructors have been marked “*deprecated*” with JDK 9 and “*deprecated, for removal*” in JDK 16. That’s quite some time and versions keeping backward compatibility and there will be some time and versions where the constructors still exist but marked as “*deprecated, for removal*”. There are good reasons to get rid of those constructors altogether. These wrapper classes are natural candidates for *value types* or for whatever future mechanism that treats these objects like primitive values, i.e. without a specific object identity. Which is incompatible with supporting `new`.
  • @Michael sure I can remember, [`LogManager.addPropertyChangeListener`](https://docs.oracle.com/javase/8/docs/api/java/util/logging/LogManager.html#addPropertyChangeListener-java.beans.PropertyChangeListener-) and `removePropertyChangeListener`, [removed in Java 9](https://docs.oracle.com/javase/9/docs/api/java/util/logging/LogManager.html#method.summary). I can remember because a) your right, “it almost never happens” and b) it happened fast, deprecated in JDK 8, removed in JDK 9. Still, I think, changing the semantics of `new` would be more disruptive than removing a few constructors.

以上是弃用的Short(String)如何更新?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>