I'm just pondering about whether or not it's possible to create one's own data type? So if you need more precision, that's not supported by one of the basic types, you could just "create" your own fullfilling your requierements.
Is that possible and how?
Take a look at BigDecimal
Immutable, arbitrary-precision signed decimal numbers
And to answer your question - yes, you can crate data types, but they can't be primitive types (like int
, double
, etc). They have to be classes, just like the case with BigDecimal
(and BigInteger
)
And a further advice for using the Big*
classes - as written, they are immutable. This means that calling add(..)
doesn't change the object - it returns a new object that reflects the change. I.e.
BigDecimal dec = BigDecimal.ZERO;
dec.add(new BigDecimal(5)); // nothing happens
dec = dec.add(new BigDecimal(5)); // this works
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With