java.math.BigInteger使用心得总结(转)
今天参考课本写了一个关于二进制与十进制转换的程序,程序算法不难,但写完后测试发现不论是二转十还是十转二,对于大于21亿即超过整数范围的数不能很好的转换。都会变成0.
参考书籍发现使用使用BigInteger可以解决这个问题。
于是查找了下JDK,然后测试几次终于写成功了!
使用心得如下:
1,BigInteger属于java.math.BigInteger,因此在每次使用前都要import 这个类。偶开始就忘记import了,于是总提示找不到提示符。
2,其构造方法有很多,但现在偶用到的有:
BigInteger(String |
BigInteger(String |
如要将int型的2转换为BigInteger型,要写为BigInteger two=new BigInteger("2"); //注意2双引号不能省略
3,BigInteger类模拟了所有的int型数学操作,如add()==“+”,divide()==“-”等,但注意其内容进行数学运算时不能直接使用数学运算符进行运算,必须使用其内部方法。而且其操作数也必须为BigInteger型。
如:two.add(2)就是一种错误的操作,因为2没有变为BigInteger型。
4,当要把计算结果输出时应该使用.toString方法将其转换为10进制的字符串,详细说明如下:
String |
toString() 返回此 BigInteger 的十进制字符串表示形式。 |
输出方法:System.out.print(two.toString());
5,另外说明三个个用到的函数。
BigInteger |
remainder(BigInteger |
BigInteger |
negate() 返回其值是 (-this) 的 BigInteger。 |
int |
compareTo(BigInteger |
remainder用来求余数。
negate将操作数变为相反数。
compare的详解如下:
compareTo
public int compareTo(BigInteger val)
- Compares this BigInteger with the specified BigInteger. This method is provided in preference to individual methods for each of the six boolean comparison operators (<, ==, >, >=, !=, <=). The suggested idiom for performing these comparisons is: (x.compareTo(y) <op> 0), where <op> is one of the six comparison operators.
-
- Specified by:
compareTo
in interfaceComparable<BigInteger>
-
- Parameters:
val
- BigInteger to which this BigInteger is to be compared.- Returns:
- -1, 0 or 1 as this BigInteger is numerically less than, equal to, or greater than val.
import java.math.BigInteger; public class BigIntegerDemo { public static void main(String[] args) {
BigInteger big=BigInteger.ONE;
System.out.println("BigInteger.ONE:"+big);
System.out.println("nextProbablePrime:"+big.nextProbablePrime());
System.out.println("nextProbablePrime:"+big.nextProbablePrime()); big=BigInteger.TEN;
System.out.println("BigInteger.TEN:"+big); big=BigInteger.ZERO;
System.out.println("BigInteger.ZERO:"+big);
} }
输出:
BigInteger.ONE:1
nextProbablePrime:2
nextProbablePrime:2
BigInteger.TEN:10
BigInteger.ZERO:0
java.math.BigInteger使用心得总结(转)的更多相关文章
- 【java.math.BigInteger】常用函数
1. /* 返回此BigInteger的函数正负号. 此方法返回-1,0或1作为此BigInteger的值对应是负,零或正数. */ java.math.BigInteger.signum(BigIn ...
- 【java.math.BigInteger】【转】常见问题
好大的链接给原作 Q: 在java怎样将BigInteger类型的数据转成int类型的? A:BigInteger的intValue()可以获得int类型数值. Q: java.math.BigInt ...
- [记录]java.math.biginteger cannot be cast to java.lang.long
可以直接使用BigInteger类型进行接收, BigInteger id = (BigInteger)QueryRunner(conn,"SELECT LAST_INSERT_ID&quo ...
- Java中的java.math.BigInteger
Java中的java.math.BigInteger /** * */ package com.you.model; /** * @author YouHaidong * */ public clas ...
- mysql连接报java.math.BigInteger cannot be cast to java.lang.Long异常
使用hibernate出现以下错误 java.sql.SQLException: java.lang.ClassCastException: java.math.BigInteger cannot b ...
- 【问题解决:连接异常】 java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long
问题描述: MySQL更新到8.0.11之后连接数据库时会报出错误 Your login attempt was not successful, try again.Reason: Could not ...
- 连接Mysql时出现java.math.BigInteger cannot be cast to java.lang.Long问题
今天遇见这样一个坑.在连接数据库进行查询数据时,大家可能会遇见这样一个问题:java.math.BigInteger cannot be cast to java.lang.Long,然后去检查代码中 ...
- Spring boot启动时报 java.sql.SQLException: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long错误
Spring boot启动时报 java.sql.SQLException: java.lang.ClassCastException: java.math.BigInteger cannot be ...
- 初学MyBatis(踩坑)Error querying database. Cause: java.sql.SQLException: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long
最近在学习Mybatis,代码全部根据教程写好了,一运行结果报了一个错误,主要错误内容: Caused by: org.apache.ibatis.exceptions.PersistenceExce ...
随机推荐
- js基础1
一.JavaScript 不同于Java 有三部分组成 核心(ECMAScript) 文档对象模型(DOM) 浏览器对象模型(BOM) 二.var 是定义数据前加的前缀 三.弹出 alert( ) ...
- HTML文本格式化
文本格式化标签: 标签 描述 <b> 定义粗体文本. <big> 定义大号字. <em> 定义着重文字. <i> 定义斜体字. <small> ...
- 在 APK 中找不到对应的 securityguard***.so 文件或者 so 文件载入出错
1.解压查看你的apk是不是将so打进去了,如果没有打进入查看自己的jnilibs是否指定 2.查看手机是否是64的手机.因为百川的安全包没有64的支持,所以目前是以兼容包的形式给的包.如果第三方有6 ...
- jquery杂记之checkbox控制select置灰
jquery: $(function(){ $("#avg_day_live").bind("click",function(){ //点击 if($(&q ...
- 简单JavaSE数据类型入门
新的一节学习了JavaSE,今天主要来说一下Java的数据类型及其输出,借用Xmind,可以生动形象的向大家来解释Java的数据类型: 其中数值整数型举例: public class A02{ pub ...
- OC 知识点回顾
/* 字符串: NSString 不可变字符串 字符串对象的内容不能修改,字符串的指针可以改变 NSMutableString 可变字符串 可以修改字符串对象的内容,继承自NSString , ...
- 浅谈break 、continue、return,goto四种语句的区别。
浅谈break .continue.return三种语句的区别: break,continue,return这三个具有跳转功能的语句在c语言中经常被用到,近期身边有些小伙伴总是把它们的用法搞乱,在这里 ...
- 近期专案PM相关收获
1, 厚黑学讲的有道理, 坏人? 为什么占便宜., 好人为什么当不了坏人是有一定道理的. -- 作为PM,能力大小居然都能胜任,从这一点上对组员不负责,如下种种都算有则改之无则加勉. ...
- javascript 去除字符串中重复字符
/** * 去除字符串中重复的字符,以下提供2种方法, * removeRepeat()为自己所想: * removeRepeat2()参考网上思路补充的 * removeRepeat3()敬请期待· ...
- 10.21_Nutz批量插入顺序,POI,wiki持续关注,POI,SSH,数据库优先
(1)Nutz,dao的批量插入会关注顺序吗? http://www.douban.com/group/topic/64322582/ (2)工作需要优先!!! POI,SSH,数据库管理及plsq ...