Solved

BigDecimal  vs String

Posted on 2014-03-25
1
466 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
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.

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now