Search code examples
javaperformancebigdecimal

BigDecimal constructor performance - string vs numeric


new BigDecimal("10000");
new BigDecimal(10000);

I know the string constructor is used if the number is bigger than the compiler would accept, but are either of the constructors faster than the other?


Solution

  • You can look at source code.

    public BigDecimal(int val) {
        intCompact = val;
    }
    
    public BigDecimal(String val) {
        this(val.toCharArray(), 0, val.length());
    }
    
    public BigDecimal(char[] in, int offset, int len) {
           ...very long
    }
    

    Obviously, who is faster.