Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Doing one's own data type? (eg. long long double?)

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?

like image 568
Andreas Hornig Avatar asked Mar 10 '10 13:03

Andreas Hornig


1 Answers

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
like image 79
Bozho Avatar answered Oct 01 '22 23:10

Bozho