Java中的基本运算符-爱代码爱编程
运算符
使用方法示例
算术运算符
public class Demo006 {
public static void main(String[] args) {
int a = 10;
int b = 20;
int c = 21;
//idea快捷键:ctrl+d,复制当前行到下一行
//四则运算
System.out.println(a+b); //30
System.out.println(a-b); //-10
System.out.println(a*b); //200
System.out.println((double) a/b); //结果为0.5,需进行强转,否则结果为int型不正确
//取模运算
System.out.println(c%a); //结果为1,( 21/10 = 2 ………… 1 )
//自增:++、自减:--
int d = a++; //先赋值再自增,自减用法相同
System.out.println(d); //结果为10
System.out.println(a); //结果为11
int e = ++a; //先自增再赋值,自减用法相同
System.out.println(e); //结果为12
System.out.println(a); //结果为12
}
}
public class Demo01 {
public static void main(String[] args) {
//测试不同类型的数据进行运算后运算结果类型的变化
long a = 123123123123123L;
int b = 123;
short c = 10;
byte d = 8;
System.out.println(a+b+c+d); //结果为long型
System.out.println(b+c+d); //结果为int型
System.out.println(c+d); //结果为int型
//数值运算中除含有long型、double型数据计算外,其余默认结果为int型
//幂运算:可使用math类计算,很多时候运算可使用工具类来操作,更加简单!
//计算3的平方,使用math工具类中的pow方法
//该方法是一个静态方法,带两个参数,其类型均为double,其中,第一个参数是底数,第二个参数是指数
double pow = Math.pow(3,2);
System.out.println(pow);
}
}
关系运算符
public class Demo02 {
public static void main(String[] args) {
//关系运算符返回值为布尔型
int a = 10;
int b = 20;
int c = 10;
System.out.println(a>b); //返回false
System.out.println(a<b); //返回true
System.out.println(a==b); //返回false
System.out.println(a>=c); //返回true
System.out.println(a<=c); //返回true
System.out.println(b!=c); //返回true
System.out.println(a!=c); //返回false
}
}
逻辑运算符
public class Demo03 {
public static void main(String[] args) {
//逻辑运算:与(and) 或(or) 非(取反)
boolean a = false;
boolean b = true;
System.out.println("a && b:"+(a&&b)); //逻辑与运算:全部都为真择为true,否则为false
System.out.println("a || b:"+(a||b)); //逻辑或运算:只要有一个是真则为true
System.out.println("!(a && b):"+!(a && b)); //逻辑非运算:取反,为真则返回假,为假则返回真
//逻辑运算中的短路运算
int c = 5;
boolean d = (c<4) && (c++>4); //c不小于4为false,不进行后面的自增和比较
System.out.println(d); //false
System.out.println(c); //5
boolean e = (c>4) && (c++>4); //c大于4为true,继续向下进行自增和比较
System.out.println(e); //false
System.out.println(c); //6
}
}
位运算
public class Demo04 {
public static void main(String[] args) {
/*
A = 0011 1100
B = 0000 1101
A&B = 0000 1100 都为1则1,否则为0
A|B = 0011 1101 只要有一个为1则为1
A^B = 0011 0001 两个数相同为0,不同则为1
~B = 1111 0010 取反,1-->0 ,0-->1
*/
//面试题:2*8 = 16怎么快速运算
/*
<<:左移,移动1位相当于把数字乘以2
>>:右移,移动1位相当于把数字除以2
0000 0000 = 0
0000 0001 = 1
0000 0010 = 2
0000 0011 = 3
0000 0100 = 4
0000 1000 = 8
0001 0000 = 16
*/
System.out.println(2<<3); //向左移动3位,即2*2*2*2,结果为16
System.out.println(3<<2); //3*2*2,结果为12
}
}
扩展运算符
public class Demo05 {
public static void main(String[] args) {
int a = 10;
int b = 20;
a += b; //相当于 a = a+b
System.out.println(a); //30
a -= b; //相当于 a = a-b,此时a的值为30
System.out.println(a); //10
a *= b; //相当于 a = a*b,此时a的值为10
System.out.println(a); //200
a /= b; //相当于 a = a/b,此时a的值为200
System.out.println(a); //10
//字符串连接符:+
System.out.println(a+b); //此时a为10,b为20,结果为30
System.out.println(""+a+b); //1020
System.out.println(a+b+""); //30
}
}
条件运算符
public class Demo06 {
public static void main(String[] args) {
//条件运算符:x ? y : z
//如果x==true,则返回y,否则返回z
int score = 80;
String llm = score < 60 ? "不及格" : "及格";
System.out.println(llm); //返回及格
}
}
运算符存在优先级,其中()的优先级最高,在一般的编程中可正确的使用()进行运算顺序的调整。
优先级图表来自:http://c.biancheng.net/view/794.html
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 本文链接: https://blog.csdn.net/qq_44774540/article/details/111088964