【20221-06-24】JAVA学习日志

2021-08-15 21:59

阅读:820

标签:不同   应该   运算   结果   加法   注意   就是   inter   解决   今日进度:学习了>Java程序基础>Java语言架构>整数运算,总进度【01|50】 【20221-06-24】JAVA学习日志 日志|发布:2021-06-24 07:18:19 更新:2021-06-24 08:19:42 源站:https://www.cnblogs.com/seto/ Rss 订阅 | 传真 | 第【06】份,总 第 09 份. Java的整数运算遵循四则运算规则,可以使用任意嵌套的小括号。四则运算规则和初等数学一致。例如: // 四则运算 整数的数值表示不但是精确的,而且整数运算永远是精确的,即使是除法也是精确的,因为两个整数相除只能得到结果的整数部分: int x = 12345 / 67; // 184 求余运算使用%: int y = 12345 % 67; // 12345÷67的余数是17 特别注意:整数的除法对于除数为0时运行时将报错,但编译不会报错。 溢出 要特别注意,整数由于存在范围限制,如果计算结果超出了范围,就会产生溢出,而溢出不会出错,却会得到一个奇怪的结果: // 运算溢出 要解释上述结果,我们把整数2147483640和15换成二进制做加法: 0111 1111 1111 1111 1111 1111 1111 1000 + 0000 0000 0000 0000 0000 0000 0000 1111 ----------------------------------------- 1000 0000 0000 0000 0000 0000 0000 0111 由于最高位计算结果为1,因此,加法结果变成了一个负数。 要解决上面的问题,可以把int换成long类型,由于long可表示的整型范围更大,所以结果就不会溢出: long x = 2147483640; long y = 15; long sum = x + y; System.out.println(sum); // 2147483655 还有一种简写的运算符,即+=,-=,*=,/=,它们的使用方法如下: n += 100; // 3409, 相当于 n = n + 100; n -= 100; // 3309, 相当于 n = n - 100; 自增/自减 Java还提供了++运算和--运算,它们可以对一个整数进行加1和减1的操作: // 自增/自减运算 Run 注意++写在前面和后面计算结果是不同的,++n表示先加1再引用n,n++表示先引用n再加1。不建议把++运算混入到常规运算中,容易自己把自己搞懵了。 移位运算 在计算机中,整数总是以二进制的形式表示。例如,int类型的整数7使用4字节表示的二进制如下: 00000000 0000000 0000000 00000111 可以对整数进行移位运算。对整数7左移1位将得到整数14,左移两位将得到整数28: int n = 7; // 00000000 00000000 00000000 00000111 = 7 int a = n 2; // 00000000 00000000 00000000 00000001 = 1 int c = n >> 3; // 00000000 00000000 00000000 00000000 = 0 如果对一个负数进行右移,最高位的1不动,结果仍然是一个负数: int n = -536870912; int a = n >> 1; // 11110000 00000000 00000000 00000000 = -268435456 int b = n >> 2; // 11111000 00000000 00000000 00000000 = -134217728 int c = n >> 28; // 11111111 11111111 11111111 11111110 = -2 int d = n >> 29; // 11111111 11111111 11111111 11111111 = -1 还有一种无符号的右移运算,使用>>>,它的特点是不管符号位,右移后高位总是补0,因此,对一个负数进行>>>右移,它会变成正数,原因是最高位的1变成了0: int n = -536870912; int a = n >>> 1; // 01110000 00000000 00000000 00000000 = 1879048192 int b = n >>> 2; // 00111000 00000000 00000000 00000000 = 939524096 int c = n >>> 29; // 00000000 00000000 00000000 00000111 = 7 int d = n >>> 31; // 00000000 00000000 00000000 00000001 = 1 对byte和short类型进行移位时,会首先转换为int再进行位移。 仔细观察可发现,左移实际上就是不断地×2,右移实际上就是不断地÷2。 位运算 位运算是按位进行与、或、非和异或的运算。 与运算的规则是,必须两个数同时为1,结果才为1: n = 0 & 0; // 0 n = 0 & 1; // 0 n = 1 & 0; // 0 n = 1 & 1; // 1 或运算的规则是,只要任意一个为1,结果就为1: n = 0 | 0; // 0 n = 0 | 1; // 1 n = 1 | 0; // 1 n = 1 | 1; // 1 非运算的规则是,0和1互换: n = ~0; // 1 n = ~1; // 0 异或运算的规则是,如果两个数不同,结果为1,否则为0: n = 0 ^ 0; // 0 n = 0 ^ 1; // 1 n = 1 ^ 0; // 1 n = 1 ^ 1; // 0 对两个整数进行位运算,实际上就是按位对齐,然后依次对每一位进行运算。例如: // 位运算 上述按位与运算实际上可以看作两个整数表示的IP地址10.0.17.77和10.0.17.0,通过与运算,可以快速判断一个IP是否在给定的网段内。 运算优先级 在Java的计算表达式中,运算优先级从高到低依次是: () ! ~ ++ -- * / % + - >>> & | += -= *= /= 记不住也没关系,只需要加括号就可以保证运算的优先级正确。 类型自动提升与强制转型 在运算过程中,如果参与运算的两个数类型不一致,那么计算结果为较大类型的整型。例如,short和int计算,结果总是int,原因是short首先自动被转型为int: // 类型自动提升与强制转型 也可以将结果强制转型,即将大范围的整数转型为小范围的整数。强制转型使用(类型),例如,将int强制转型为short: int i = 12345; short s = (short) i; // 12345 要注意,超出范围的强制转型会得到错误的结果,原因是转型时,int的两个高位字节直接被扔掉,仅保留了低位的两个字节: // 强制转型 因此,强制转型的结果很可能是错的。 练习 计算前N个自然数的和可以根据公式: \frac{(1+N)\times N}22(1+N)×N 请根据公式计算前N个自然数的和: // 计算前N个自然数的和 public class Main { public static void main(String[] args) { int n = 100; System.out.println(sum); System.out.println(sum == 5050 ? "测试通过" : "测试失败"); } } 小结 整数运算的结果永远是精确的; 运算结果会自动提升; 可以强制转型,但超出范围的强制转型会得到错误的结果; 应该选择合适范围的整型(int或long),没有必要为了节省内存而使用byte和short进行整数运算。 ----the END-------------------------------------------------------------------------------------------------------- Statement Copyright of this article is the original author (s) and the original author (s).Content is the author‘s personal view and does not represent the station as endorsing its views and as accountable for its authenticity, and the sole reference to this station does not constitute any investment and application recommendations.This site is a platform for personal study communication, and part of the articles on the site are reprinted and not used for any commercial purpose, we have notified authors and sources as much as possible, but have limited capacity or negligence to cause missed publication, please contact us promptly, and we will correct or delete the content immediately upon request from the copyright holder.This station holds the final interpretation of this statement. Statement: the article is reprinted, reproduced on board for the purpose of wider dissemination of marketing information, and does not represent an agreement with the company on its views.The contents of the article are for information only.【20221-06-24】JAVA学习日志标签:不同   应该   运算   结果   加法   注意   就是   inter   解决   原文地址:https://www.cnblogs.com/seto/p/Java-06-24.html


评论


亲,登录后才可以留言!