Java学习日记(三)运算符
运算符包含很多种,加减乘除,取模运算就不再进行整理了。
在Java中新学习到了&&有短路的功能,还有四个位运算符 & | ^ ~这四个运算符
还有左移 << 右移>>
`
public class Demo05 {
public static void main(String[] args) {
boolean a = true;
boolean b = false;
//&& 和(and)都为真的时候结果为真,否则为假
System.out.println("a&&b :"+(a&&b));
//|| 或运算(or) 一真则真
System.out.println("a||b :"+(a||b));
//! 非,对运算结果取反
System.out.println("!(a&&b):"+!(a&&b));
//&& 令一个操作:短路
int c = 10;
boolean d = c<10;
System.out.println(d&&c++<10);//false
System.out.println(c);//10 为什么c没有执行自增的操作,原因就是&&前为false,所以不再执行&&后面的自增操作
//位运算符
/*
* A=0011 1001
* B=1010 0010
*
* A&B=0010 0000
* A|B=1011 1011
* A^B=0110 0100 A和B对应位置上相等,就为1
* ~B=0101 1101 就是对B进行取反
*
* <<左移 *2
* >>右移 /2*/
System.out.println(2<<3);//16
System.out.println(16>>3);//2
}
}
自增自减以及字符串链接
`
public class Demo06 {
public static void main(String[] args) {
// += -=
int a = 30;
int b = 10;
a+=b;//a=a+b
a-=b;//a=a-b
System.out.println(a);//30
//字符串连接符 +
System.out.println(""+a+b);//3010
System.out.println(a+b+"");//40
System.out.println(a+""+b);//3010
//字符串类型放的位置不一样结果也不一样
int c = 1;
System.out.println(a+c+""+b+c);//31101 字符串前面做了加法运算,而字符串后面做了连接
}
}
三元运算符
`
public class Demo07 {
public static void main(String[] args) {
//三元运算符
// x ? y : z
//如果x==true 结果为y 否则结果为假
int x = 80;
String s = x < 90 ? "x小于90" : "x不小于90";
System.out.println(s);//x小于90
}
}