企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持知识库和私有化部署方案 广告
### 概述 java中long型为最大整数类型,对于超过long型的数据如何去表示呢.在Java的世界中,超过long型的整数已经不能被称为整数了,它们被封装成BigInteger对象.在BigInteger类中,实现四则运算都是方法来实现,并不是采用运算符. ### BigInteger构造器 **推荐使用字符串作为传入构造器的参数**.要求数字格式,没有长度限制 . ~~~ BigInteger x = new BigInteger("123456789012345678956789012345678901234567890"); System.out.println(x); //完全超过long的数值范围了 ~~~ 打印结果 ~~~ 123456789012345678956789012345678901234567890 //这还是一个BigInteger的对象 ~~~ ### 四则运算 #### 加 ~~~ BigInteger x = new BigInteger("123456789012345678956789012345678901234567890"); BigInteger y = new BigInteger("123456789012345678956789012345678901234567890"); System.out.println(x.add(y)); ~~~ 结果 ~~~ 246913578024691357913578024691357802469135780 ~~~ #### 减 ~~~ BigInteger x = new BigInteger("123456789012345678956789012345678901234567890"); BigInteger y = new BigInteger("123456789012345678956789012345678901234567890"); System.out.println(x.subtract(y)); //x-y ~~~ 结果 ~~~ 0 ~~~ #### 乘 ~~~ multiply() ~~~ #### 除 ~~~ divide() ~~~