Java运算符使用、单目、二目运算符、数据类型精度运算问题等 |
您所在的位置:网站首页 › 不属于excel中的算术运算符号的是 › Java运算符使用、单目、二目运算符、数据类型精度运算问题等 |
前言:
Java提供了丰富的运算符,例如算术运算符、关系运算符、逻辑运算符、位运算符等; 其中又分有单目运算符和二目运算符; 一、单目运算符:只对单独一个操作元(变量)进行操作;-------- 其中自增、自减运算符++、--就是典型的单目运算符; 自增、自减运算符的使用特点: 1、他们在使用时可以放在操作元之前,也可以放在操作元之后,不过所蕴含的意义是不一样的; --- 当放在操作元之前,如 ++x ; (--x ;) 代表在使用 x 之前,先使 x 的值加 1(减1);以下代码进行演示: public static void main(String[] args) { int x = 1; ++x; System.out.println(x); //输出x为2 --x; System.out.println(x); //输出x为1 int y = 10; System.out.println(y); //输出y为10 y = ++x; //x是先进行加1操作,再将值赋给y; System.out.println(y); //输出y为2 System.out.println(x); //输出x为2 }--- 当放在操作元之后,如 x++ ; (x-- ;) 代表在使用 x 之后,再使 x 的值加 1(减1);以下代码进行演示: public static void main(String[] args) { int x = 1; x++; System.out.println(x); //输出x为2 x--; System.out.println(x); //输出x为1 int y = 10; System.out.println(y); //输出y为10 y = x++; //x是先将值赋给y之后,才进行加1操作的; System.out.println(y); //输出y为1 System.out.println(x); //输出x为2 }2、操作元必须是一个整数类型或者浮点类型的变量,作用都是使变量的值加1或者减1; 二、二目运算符:同时对两个操作元(变量)进行操作;-------- 二目运算符有: 加减运算符+,-乘除运算符*,/求余运算符%关系运算符> , < , >= , , < , >= , 10 ; 关系不成立,结果为false ; 5>1 ; 关系成立,结果为true ;2、关系运算符中,大小关系运算:> , < , >= , 4+5 ; 赋值运算符: = ;1、对于赋值运算符,右边的操作元必须是变量,不能是常量或者表达式; 如:x = 10 ; y = 11 ; 正确, 不能是 10 = x ;11 = y; 2、赋值运算符的优先级为14级; 3、结合计算的顺序是从右到左的; 4、赋值运算符不能跟关系运算符中的 == 等号关系运算符混淆;如 12 = 12 ; 这是错误的; 逻辑运算符:&& , || , ! ; (逻辑与、逻辑或、逻辑非)1、逻辑与 (&& )优先级为11,当运算符左右两边的表达式都为真时,整个表达式的值才为真; 如:if( top1>0 && top2>5 ) 当 top1>0 为真 , top2>5 为真 时 ,则if中表达式的值才为真; 扩展:if( top1>10 && top210 为假时 , 则不继续判断 top210 && top210 || top2>15 ) 当 top1>10 为假 , 则继续对 top2>15 进行判断 ,当top2>15为真,则整个表达式的值为真; 扩展:if ( top1>10 || top2>50 ) 当 top1>10 为真时 , 则不继续判断 top1>50 ,默认表达式为真 ;以下代码进行演示: int top1 = 100 , top2 = 20; if(top1>10 || top2>50){ System.out.println(top1); //逻辑表达式为真则输出top1 }else{ System.out.println(top2); //逻辑表达式为假则输出top2 }3、逻辑非( ! )优先级为2 ,将逻辑表达式的值变成相反的值; 以下代码进行演示:top1>10 为真,使用逻辑非之后 !(top1>10) 则为假,最后结果输出 20 ; int top1 = 100 , top2 = 20; if( !(top1>10) ){ System.out.println(top1); //逻辑表达式为真则输出top1 }else{ System.out.println(top2); //逻辑表达式为假则输出top2 } 扩展: 不同类型之间运算时的精度问题基本数据类型的精度从 “ 低 ” 到 “ 高 ” 排列: byte、short、char、int、long、float、double 1、当表达式中有 double 类型的运算时,则按 double 类型进行运算; 如:5.6 / 2 +10 ,结果输出 12.8 ; 2、当表达式中最高精度为 float 类型的运算时,则按 float 类型进行运算; 如:6.6f / 2 +10 ,结果输出 13.3 ;此时的 13.3 是 float 类型的; double intd = 5.6/2 + 10 ; System.out.println(ints); //输出 12.8 float intf = 6.6f/2 + 10 ; //输出 13.3 System.out.println(intf); intf = intd ; //报错Type mismatch: cannot convert from double to float3、当表达式中最高精度为 long 类型的运算时,则按 long 类型进行运算; 如 :12L+9*2 , 结果输出 30 ;此时的 30 是 long 类型的; 4、当表达式中最高精度为 int 类型或者低于 int 类型的运算时,则按 int 类型进行运算; 如 :20 + 15 * 2 ,结果输出 50 ;此时的 50 是 int 类型的; int intt = (byte) 10 + 'a' ; 结果输出 107 ;此时的 107 是 int 类型的; 特别注意:Java中不能把超过 byte 、short 、char 的取值范围的算术表达式的值赋给 byte 、short 、char 类型的变量;如:byte intb = 20 + 'a' ; 结果输出 117 ;此时的 117是int类型的常量 ; 因为117没有超过byte的取值范围,所以程序编译通过; 当:byte intbb = 40 + 'a' ; // 报错:Type mismatch: cannot convert from int to byte 因为此时 40 + 'a' 输出结果为 137 ,已经超过了byte的取值范围,所以程序报错; 需要详细了解为什么int类型常量可以直接赋予byte类型变量可以访问下面链接: https://blog.csdn.net/LagerSwan/article/details/104061692 |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |