0

How can I convert BigInteger value to BigDecimal without using new operator?

For instance, if I have an integer value like:

abc=4000. 

I should get the output as:

xyz= 4000.0
Biffen
  • 6,249
  • 6
  • 28
  • 36
Vishnu
  • 176
  • 1
  • 2
  • 19
  • 1
    you can't create a *new* BigDecimal without using the *new* operator (or if there is a way, then a new BigDecimal *will* be created anyway - you just won't see it)... – assylias Feb 06 '15 at 11:46
  • no, you cant. Look at the answer below. It is impossible to create objects without `new` in java. – specializt Feb 06 '15 at 11:49
  • It depends on what `without using new operator` means. No use in own code or no use in internal code. – Jens Feb 06 '15 at 11:51
  • You cannot convert, you need to create new `BigDecimal` instance. About creating objects without `new` operator read http://stackoverflow.com/questions/95419/what-are-all-the-different-ways-to-create-an-object-in-java – Sergey Fedorov Feb 06 '15 at 12:30

2 Answers2

5

use valueOf if you mean that your own code should not use new operator.

BigDecimal.valueOf(abc.longValue());

if abc is a BigInteger.

If you want to create the object without using new in your code and in the code of the jre then you can not do it. BigDecimal.valueOf() uses new to create a BigDecimal object.

Jens
  • 67,715
  • 15
  • 98
  • 113
  • @assylias Yes internally this funktion will do it. But I think the meaning of OP's question is that he will not use it in this code. – Jens Feb 06 '15 at 11:50
0

No you cant, you can do it only with primitive types, everyting else is an object and has to be instantiate by using new operator. It might use it "under the hood" in example wrapped in factory method like valueOf for BigDecimals but it will be still using it.

user902383
  • 8,420
  • 8
  • 43
  • 63