Java BigDecimal 与 Android BigDecimal

Java BigDecimal vs Android BigDecimal

我遇到过 java.math.BigDecimalandroid.icu.math.BigDecimal,因为我需要在项目中使用 BigDecimal

我发现 Android BigDecimal 需要 API 24 级

这两个有什么区别类? Android实现中有没有性能优化?

我认为 TLDR 的区别只是 java.math.BigDecimal 用于 Java 和 android.icu.math.BigDecimal 适用于 Kotlin。

您可以阅读他们的文档以了解更详细的差异。

https://developer.android.com/reference/java/math/BigDecimal https://developer.android.com/reference/kotlin/android/icu/math/BigDecimal

无论使用java.math.BigDecimal还是android.icu.math.BigDecimal,两者在性能上不会有差异。因为这些是基本的 class,所以 Android BigDecimal 是参考 Java BigDecimal 重新创建的。

如果你问两者的区别,那么

  • 两者的目的相同。
  • 根据代码,它们可能不同,可以有更少或更多的方法。所以你应该阅读这两个 classes 中的所有方法来找出区别。

https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html https://developer.android.com/reference/java/math/BigDecimal

Android 重新创建 classes 以向现有 classes 添加一些功能。

android.icu.math.BigDecimalICU library (International Components for Unicode). Since API 24 the Android framework exposes some of the ICU4J classes for you to use. The Android documentation on Unicode and I18N support 状态的一部分:

Some classes in the java and android packages have equivalents to those found in ICU4J. However, ICU4J often provides broader support for standards and languages.

不要被上面的引述所迷惑,因为 ICU 类 确实位于 android 包中。为了避免冲突,它们已从 com.ibm.icu 移至 android.icu

特别是关于 BigDecimalI found a discussion on the internet 得出的结论是 BigDecimal 在 Java 中可用之前在 ICU 中可用,并出于兼容性目的保留在库中.

底线:使用您喜欢的任何实现,但 java.math.BigDecimal 将来不太可能被弃用。