Solved

BigDecimal  vs String

Posted on 2014-03-25
1
503 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
1 Comment
 
LVL 35

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 213
web services creation SOAP vs REST 5 57
CSV file parsing thru Java 13 34
maven not picking latest jar instead picking old jar from .m2 12 22
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.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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 explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

828 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