Mosius
Mosius

Reputation: 1682

Java BigDecimal vs Android BigDecimal

I've faced java.math.BigDecimal and android.icu.math.BigDecimal as I require to use BigDecimal in the project.

I've realized that the Android BigDecimal requires API level 24

What is the different between these two classes? I wonder if there is any performance optimization in Android implementation?

Upvotes: 11

Views: 2300

Answers (3)

Khemraj Sharma
Khemraj Sharma

Reputation: 59004

Either use java.math.BigDecimal or android.icu.math.BigDecimal, there will not be performance difference between both. Because these are basic class, Android BigDecimal was re-created with reference of Java BigDecimal.

If you ask difference between both, then

  • by purpose both are same.
  • by code they may differ, can have some less or more methods. So you should read all methods in both classes to find out difference.

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

Android re-create classes to add some feature to existing classes.

Upvotes: 1

Markus Penguin
Markus Penguin

Reputation: 1661

android.icu.math.BigDecimal is part of the ICU 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 states:

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.

Don't be confused by the above quote because ICU classes do reside in the android package. They were moved to android.icu from com.ibm.icu in order to avoid conflicts.

Regarding BigDecimal in particular: I found a discussion on the internet which concludes that BigDecimal was available in ICU before it was available in Java and is kept in the library for compatibility purposes.

Bottom line: use whatever implementation you like, but java.math.BigDecimal is less likely to be deprecated in the future.

Upvotes: 11

IsaiahJ
IsaiahJ

Reputation: 454

I think the TLDR difference is just that java.math.BigDecimal is for Java and android.icu.math.BigDecimal is for Kotlin.

You could read up on their documentation for more detailed differences.

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

Upvotes: -5

Related Questions