Solved

BigDecimal  vs String

Posted on 2014-03-25
1
532 Views
Last Modified: 2014-04-13
for accurate and of unlimited size and precision one should use a BigDecimal.

Java 7 operator overloading for BigDecimal make  code more readable

I have not understood why BigDecimal used how it is more useful compared to String. What are advantages, disadvantage, practical uses of BigDecimal.

please advise
Any links resources ideas highly appreciated. Thanks in advance
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 36

Accepted Solution

by:
mccarl earned 500 total points
ID: 39954756
I have not understood why BigDecimal used how it is more useful compared to String
Comparing BigDecimal to String makes no sense as they are used in totally different ways. A String is used to store character data whereas a BigDecimal is used to store a decimal number. Yes, you could store your decimal number as the digit characters in a String, but BigDecimal provides all the functionality that you might expect to be able to apply to numbers, such as addition, substraction, multiplication, division, etc.

If you were to compare BigDecimal to double, that would be more meaningful. The differences between those two types, are probably many but I would highlight 3...

The ability of BigDecimal to store/represent decimal numbers to an arbitrary precision. This means that if you had a number that was say, 623149613498134.23412837123876197263497612349716496961234871243907124805081756561496132465619842 for example, you can accurately represent this using a BigDecimal whereas you can't with a plain double type.
The memory storage requirements of the two are totally different. A double stores it's value in 8 bytes of memory, whereas the BigDecimal uses a variable amount of memory. For example, the number above would require much more than 8 bytes to store it in.
The other main difference is the fact that a double is a primitive value and has built in Java support for a lot of arithmetic operations. So you can do this...  double result = doubleA + doubleB;     Whereas, if you are using BigDecimal's, then you are dealing with objects and you can just add them like the above. You would need to do something like this...   BigDecimal result = bigDecimalA.add(bigDecimalB);
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question