数值类型的运算方法总结

数值类型

整   型

字节型(byte)占1个字节;短整型(short)占2个字节;整型(int)占4个字节;长整型             (long)占8个字节

浮点型

单精度浮点数(float)占4个字节;双精度浮点型(double)占8个字节

运算符

基本的算数运算符

加( + )减( - ) 乘( * )除( / )取余( % )

整数类型运算时的类型溢出问题

产生原因

固定范围:在Java中整数类型存在范围限制(int的范围是:-2^31至2^31-1)当运算结果超出该范围,则会产生溢出; 算术运算:任何可能产生超出范围结果的算术运算(加法、减法、乘法)都可能导致溢出; 类型转换:将一个范围较大的整数类型强制转换为范围较小的整数类型时,如果值超出目标类型的范围,也会发生溢出。

解决办法:整数类型运算时遇到的类型溢出问题,可以使用BigInteger解决

//BigInteger:超大整数(解决问题:普通整数再计算时,产生类型溢出的问题;保存超大整数)
        //创建时,建议用String类型保存超大整数
        BigInteger n1 = new BigInteger(String.valueOf(Integer.MAX_VALUE));
        BigInteger n2 = new BigInteger("89452304856230141252652034852356");
        //计算:add: 加    subtract: 减     multiply: 乘       divide: 除
        BigInteger ret1 = n1.add(n2);
        BigInteger ret2 = n1.subtract(n2);
        BigInteger ret3 = n1.multiply(n2);
        BigInteger ret4 = n1.divide(n2);
        //比较
        System.out.println(n1.compareTo(n2));
        System.out.println(n1.equals(n2));
        //类型转换  pow(乘方)
        BigInteger n3 = new BigInteger("99999999999999").pow(99);
        float f =n3.floatValue();
        System.out.println(n3);
        System.out.println(f);         //Infinity 无穷大
        int i = n2.intValue();        //类型溢出
        System.out.println(i);
        int i1 = n2.intValueExact();        //如果类型溢出,则抛出异常
        System.out.println(i1);

浮点类型运算时的精度丢失问题

产生原因

计算机内部是通过二进制形式来保存浮点数的,无法精确表示十进制的小数,因此会产生精度丢失的问题

解决办法:浮点数类型运算时遇到精度丢失问题,可以使用BigDecimal来解决

//BigDecimal:(解决问题: 浮点数计算时,精度丢失的问题,超大浮点数的保存)
        double n1 = 0.2;
        double n2 = 0.1;
        System.out.println(n1+n2);
        //创建
        BigDecimal d1 = new BigDecimal("10");
        BigDecimal d2 = new BigDecimal("3");
        //计算(add subtract multiply)
        System.out.println(d1.add(d2));
        System.out.println(d1.subtract(d2));
        System.out.println(d1.multiply(d2));
        //计算:divide   di.divide(d2):  算数逻辑异常(ArithmeticException)
        System.out.println(d1.divide(d2,3, RoundingMode.HALF_UP)); //保留3位小数,并设置模式
        //除法+取余
        BigDecimal[] rets = d1.divideAndRemainder(d2);
        System.out.println("商是:"+rets[0]);
        System.out.println("余数是:"+rets[1]);

输出结果:

0.30000000000000004
13
7
30
3.333
商是:3
余数是:1

注意

  1. 十进制小数转二进制的方式:整数部分除二取余,小数部分乘二取整(在该过程中,会产生无线循环的情况)
  2. 整数运算在除数为0时会抛异常,而浮点数在除数为0时,不会报错但会返回特殊值
    double d1 = 0.0 / 0;     // NaN          Not a Number
    double d2 = 1.0 / 0;     // Infinity     无穷大
    double d3 = -1.0 / 0;    // -Infinity    负无穷大

关系运算符

>    <    >=    <=    ==    !=

判断数据是否满足条件,最终返回一个结果(boolean类型:true/false)

位运算

位运算是按照整数的二进制位进行移位、与、或、非、异或运算,应用于int、long、short、char、byte等数据类型(浮点型不能进行位运算),使得计算性能得到了提高。

位运算符

移位:左移(<<)   右移(>>) :左移实际上执行乘法,右移实际执行除法

int n = 28 ;      //00000000 00000000 00000000 00011100 = 28
//右移 
int a = n >> 2;   //00000000 00000000 00000000 00000111 = 7 = 28 / 2^2
//左移
int a = n << 1;   //00000000 00000000 00000000 00111000 = 56 = 28 * 2^1

与运算:&    两数同时为1,结果为1

或运算:|      任意一个为1 ,结果为1

非运算:-      0与1互换(反转)  

异或运算:^   两数不同,结果为1,否则结果为0

常见用途

移位运算:计算指定数值的倍数

int n = 20;

//计算n的2倍

int a = n << 1;

//计算n的50%

int b = n >> 1;

与运算:判断该整数的奇偶数(该整数与1进行与运算,结果为0(偶数);结果为1(奇数))

异或运算:交换变量值

int a = 10;

int b = 5;
 

System.out.println("交换前:");

System.out.println("a = " + a);

System.out.println("b = " + b);
 

// 使用异或运算交换a和b的值

a = a ^ b;

b = a ^ b;

a = a ^ b;
 
System.out.println("交换后:");

System.out.println("a = " + a);

System.out.println("b = " + b);

逻辑运算符

短路与( && )   短路或( || )     非( ! ) 

短路与:左边false,右边不执行

短路或:左边true,右边不执行

三元运算符

条件表达式?值1:值2;

当表达式为真时,返回值1,为假时,返回值2

        //3个变量排序
        int a = 0, b = 1, c = 10;
        //最大值
        int max = (a > b) ? (a > c ? a : c) : (b > c ? b : c);
        //最小值
        int min = (a < b) ? (a < c ? a : c) : (b < c ? b : c);
        //中间值
        int mid = (a + b + c) - max - min;

        System.out.println(max);
        System.out.println(mid);
        System.out.println(min);
  • 25
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值