BigDecimal - to use new or valueOf?

For BigDecimal : this is a bit tricky, because they don't do the same thing BigDecimal. ValueOf(double) will use the canonical String representation of the double value passed in to instantiate the BigDecimal object. In other words: The value of the BigDecimal object will be what you see when you do System.out.

Println(d) If you use new BigDecimal(d) however, then the BigDecimal will try to represent the double value as accurately as possible This will usually result in a lot more digits being stored than you want. Strictly speaking, it's more correct than valueOf() but it's a lot less intuitive There's a nice explanation of this in the JavaDoc: The results of this constructor can be somewhat unpredictable. One might assume that writing new BigDecimal(0.1) in Java creates a BigDecimal which is exactly equal to 0.1 (an unscaled value of 1, with a scale of 1), but it is actually equal to 0.1000000000000000055511151231257827021181583404541015625.

This is because 0.1 cannot be represented exactly as a double (or, for that matter, as a binary fraction of any finite length). Thus, the value that is being passed in to the constructor is not exactly equal to 0.1, appearances notwithstanding In general if the result is the same (i.e. Not in the case of BigDecimal but in most other cases), then valueOf() should be preferred: it can do caching of common values (as seen on Integer.valueOf() ) and it can even change the caching behaviour without the caller having to be changed new will always instantiate a new value, even if not necessary (best example: new Boolean(true) vs Boolean.

ValueOf(true) ).

" For BigDecimal: this is a bit tricky, because they don't do the same thing.BigDecimal. ValueOf(double) will use the canonical String representation of the double value passed in to instantiate the BigDecimal object. In other words: The value of the BigDecimal object will be what you see when you do System.out.

Println(d). If you use new BigDecimal(d) however, then the BigDecimal will try to represent the double value as accurately as possible. This will usually result in a lot more digits being stored than you want.

Strictly speaking, it's more correct than valueOf(), but it's a lot less intuitive. There's a nice explanation of this in the JavaDoc: The results of this constructor can be somewhat unpredictable. One might assume that writing new BigDecimal(0.1) in Java creates a BigDecimal which is exactly equal to 0.1 (an unscaled value of 1, with a scale of 1), but it is actually equal to 0.1000000000000000055511151231257827021181583404541015625.

This is because 0.1 cannot be represented exactly as a double (or, for that matter, as a binary fraction of any finite length). Thus, the value that is being passed in to the constructor is not exactly equal to 0.1, appearances notwithstanding. In general, if the result is the same (i.e.

Not in the case of BigDecimal, but in most other cases), then valueOf() should be preferred: it can do caching of common values (as seen on Integer.valueOf()) and it can even change the caching behaviour without the caller having to be changed. New will always instantiate a new value, even if not necessary (best example: new Boolean(true) vs. Boolean. ValueOf(true)).

1 Awesome answer (+1) – Sean Patrick Floyd Aug 25 at 7:45.

Basically valueOf(double val) just does this: return new BigDecimal(Double. ToString(val)); Therefore -> yep, a new object will be created :). In general I think it depends upon your coding style.

I would not mixure valueOf and "new", if both are the same outcome.

4 Technically true, but: it'll make a huge difference. ValueOf() has the more intuitive behaviour, while new BigDecimal(d) has the more correct one. Try both and see the difference.

– Joachim Sauer Aug 25 at 6:58 Technically false. 'new' always keyword always creates a new object while the javadoc does not tell if valueOf will return always a new object or not. It does not, not always.It has some values in cache so new BigDecimal(1)!

= new BigDecimal(1) but BigDecimal. ValueOf(1) == BigDecimal. ValueOf(1) – user270349 Aug 25 at 7:43 @user: yes, but since BigDecimal is immutable it should be treated the same way that the primitive wrappers (Integer, Byte, ...) and String are treated: object identity should not matter to your code, only the value should matter.

– Joachim Sauer Aug 25 at 8:21 @Joachim Right but that internal cache is there for a reason. Too many not needed equal instances of BigDecimal are not a good thing to have. And I was answering to Dr, He said "a new object will be created" – user270349 Aug 25 at 8:48 1 @user: yes, that's why I said that valueOf() should generally be preferred.

But note that BigDecimal. ValueOf(double) doesn't do any caching (and it probably wouldn't be worth it, either). – Joachim Sauer Aug 25 at 8:50.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions