基本运算操作
+ - */ %
=
自增自减
public class Hello{
public static void main(String arg[]){
int x = 20;
int y = 30;
System.out.println("x" + "+" + "y"+ "=" + (x+y));
System.out.println("x" + "-" + "y"+ "=" + (x-y));
System.out.println("x" + "*" + "y"+ "=" + (x*y));
System.out.println("x" + "/" + "y"+ "=" + (x/y));
System.out.println("x" + "%" + "y"+ "=" + (x%y));
}
};
结果
x+y=50
x-y=-10
x*y=600
x/y=0
x%y=20
简便运算:
++ -- 主要为了坚决空间问题
++放在前面表示先进行自增,再计算,反之,则先计算,再自增。
比较如下:
public class Hello{
public static void main(String arg[]){
int x = 20;
int y = 30;
System.out.println(x++);
System.out.println(x);
}
};
结果为 20,21
public class Hello{
public static void main(String arg[]){
int x = 20;
int y = 30;
System.out.println(++x);
System.out.println(x);
}
};
结果为21,21
关系运算符:
有 >=, <= ,!=...返回的为布尔类型的值。
public class Hello{
public static void main(String arg[]){
int x = 20;
int y = 30;
System.out.println(x
}
};
结果为 true。
逻辑运算符:
可以将多个关系运算符进行统一的操作,使用逻辑运算。
与: &, &&
&表示所有的判断都进行判断
&&表示第一个是假的,后面都不进行判断了。
或:
|,||
| 即使第一个条件满足了,也会去检查所有的条件;
|| 如果第一个条件满足,则后面的就不进行判断了。
public class Hello{
public static void main(String arg[]){
if(1/1==0 & 1/0==0){
System.out.println("ok");
}
}
};
error:
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Hello.main(Hello.java:4)
public class Hello{
public static void main(String arg[]){
if(1/1==0 && 1/0==0){
System.out.println("ok");
}
}
};
结果为空
public class Hello{
public static void main(String arg[]){
if(1/1==1 | 1/0==0){
System.out.println("ok");
}
}
};
结果为
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Hello.main(Hello.java:4)
public class Hello{
public static void main(String arg[]){
if(1/1==1 || 1/0==0){
System.out.println("ok");
}
}
};
结果为
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Hello.main(Hello.java:4)
位运算(理解)
在普通的开发中,位运算很少出现。操作符有:\ & ,》》,《《
public class Hello{
public static void main(String arg[]){
int x = 3;
int y = 5;
System.out.println(x|y);
}
};
结果为7.
到二进制
3:0000000 0000000 0000000 00000011
5:0000000 0000000 0000000 00000101
x 0000000 0000000 0000000 00000111
x的十进制是7.
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/22117706/viewspace-626075/ ,如需转载,请注明出处,否则将追究法律责任。