?
Solved

BigDecimal  vs String

Posted on 2014-03-25
1
Medium Priority
?
603 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 36

Accepted Solution

by:
mccarl earned 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month15 days, 19 hours left to enroll

850 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