Solved

BigDecimal  vs String

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

734 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