今天参考课本写了一个关于二进制与十进制转换的程序,程序算法不难,但写完后测试发现不论是二转十还是十转二,对于大于21亿即超过整数范围的数不能很好的转换。都会变成0.
参考书籍发现使用使用BigInteger可以解决这个问题。
于是查找了下JDK,然后测试几次终于写成功了!
使用心得如下:

1,BigInteger属于java.math.BigInteger,因此在每次使用前都要import 这个类。偶开始就忘记import了,于是总提示找不到提示符。

2,其构造方法有很多,但现在偶用到的有:

BigInteger(String val)
           将 BigInteger 的十进制字符串表示形式转换为 BigInteger。
BigInteger(String val, int radix)
           将指定基数的 BigInteger 的字符串表示形式转换为 BigInteger。

如要将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 val)
           返回其值为 (this % val) 的 BigInteger。
BigInteger negate()
           返回其值是 (-this) 的 BigInteger。
int        compareTo(BigInteger val)
           将此 BigInteger 与指定的 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 interface Comparable<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使用心得总结(转)的更多相关文章

  1. 【java.math.BigInteger】常用函数

    1. /* 返回此BigInteger的函数正负号. 此方法返回-1,0或1作为此BigInteger的值对应是负,零或正数. */ java.math.BigInteger.signum(BigIn ...

  2. 【java.math.BigInteger】【转】常见问题

    好大的链接给原作 Q: 在java怎样将BigInteger类型的数据转成int类型的? A:BigInteger的intValue()可以获得int类型数值. Q: java.math.BigInt ...

  3. [记录]java.math.biginteger cannot be cast to java.lang.long

    可以直接使用BigInteger类型进行接收, BigInteger id = (BigInteger)QueryRunner(conn,"SELECT LAST_INSERT_ID&quo ...

  4. Java中的java.math.BigInteger

    Java中的java.math.BigInteger /** * */ package com.you.model; /** * @author YouHaidong * */ public clas ...

  5. mysql连接报java.math.BigInteger cannot be cast to java.lang.Long异常

    使用hibernate出现以下错误 java.sql.SQLException: java.lang.ClassCastException: java.math.BigInteger cannot b ...

  6. 【问题解决:连接异常】 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 ...

  7. 连接Mysql时出现java.math.BigInteger cannot be cast to java.lang.Long问题

    今天遇见这样一个坑.在连接数据库进行查询数据时,大家可能会遇见这样一个问题:java.math.BigInteger cannot be cast to java.lang.Long,然后去检查代码中 ...

  8. 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 ...

  9. 初学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 ...

随机推荐

  1. ThinkPHP函数详解:R方法

    R方法用于调用某个控制器的操作方法,是A方法的进一步增强和补充.关于A方法的用法见这里.R方法的调用格式:R('[项目://][分组/]模块/操作','参数','控制器层名称') 例如,我们定义了一个 ...

  2. UNIX线程之间的关系

    我们在一个线程中经常会创建另外的新线程,如果主线程退出,会不会影响它所创建的新线程呢?下面就来讨论一下. 1.  主线程等待新线程先结束退出,主线程后退出.正常执行. 示例代码: #include & ...

  3. Orchard 学习-手动安装Orchard

    通过Orchard zip 文件手动配置网站 这篇文章将引导你如果通过Zip文件来安装Orchard. 我们会使用三种不同的方法来承载Orchard: IIS. WebMatrix and IIS E ...

  4. iOS 查找字符串 相同 子字符串的位置 range

    问题:解决替换同一个字符串的多个相同的字符eg. xxx这个超级大土豪白送xxx一个!赶快来抢把! 将第一个xxx换成名字 将第二个xxx换成物品 两种办法    第二种办法更灵活一点 //第一种办法 ...

  5. JavaScript之String()和.toString()

    JS中 转换字符串的方法有两个 一个String(),一个.toString(). 通常情况下 这两种使用没有太大的区别.但是需要注意几点: undefined: toString() var tes ...

  6. html表格 第五节

    表格: <html> <head> <title>表格实例</title> </head> <body> <center& ...

  7. Mysql JDBC 连接串参数说明

    MySQL的 JDBC URL 格式 for  Connector/J 如下例: jdbc:mysql://[host:port],[host:port].../[database][?参数名1][= ...

  8. ASP.NET 优化 check list

    看到一个蛮有意思的网站,里面有针对asp.net方方面面优化的罗列: 点击打开链接http://webdevchecklist.com/asp.net/performance/ 点击打开链接http: ...

  9. 关于学习HTML5中自己犯的错误

    7.1写错了 siblings()这个函数写成了sibling,在jQuery中并没有这个函数的定义 在查找错误的过程中,自己也发现了一个学习jQuery的网站http://www.365mini.c ...

  10. PHP类中的__get()和__set函数到底有什么用?

    当试图获取一个不可达变量时,类会自动调用__get. 同样的,当试图设置一个不可达变量时,类会自动调用__set. 在网站中,这两个并不是什么非用不可的函数.   例如: Class Test {   ...