首页 > 其他分享 >运算符

运算符

时间:2022-10-14 10:33:53浏览次数:47  
标签:int System 运算符 println public out

运算符operator

1、二元运算符

public class Demo01 {
   public static void main(String[] args) {
       //二元运算符
       int a = 10;
       int b = 20;
       int c = 30;
       int d = 40;

       System.out.println(a-b);
       System.out.println(a+b);
       System.out.println(a*b);
       System.out.println((double)a/b);//结果为小数强转为double
  }
}

注意点:1、若结果有小数,那么必须使用强制转化(double)或者(float)

 

 

2、运算结果类型

public class Demo02 {
   public static void main(String[] args) {
       long a = 1212121121212L;
       int b = 123;
       short c = 10;
       byte d = 8;
       double e= 2.11;

       System.out.println(a+b+c+d);//long
       System.out.println(b+c+d);//int
       System.out.println(c+d);//int
       System.out.println(c+e);//double
       //有long参与运算,结果一定为long
       //有double float 参与结果为double float
       //其他的结果为int
  }
}

注意点:1、参与的运算类型里面有long,结果一定为long

2、参与的运算类型里面有double,结果为double

3、其他的结果为int

3、关系运算符返回的结果

public class Demo03 {
   public static void main(String[] args) {
       //关系运算符返回的结果: 正确 错误
       int a = 10;
       int b = 22;

       System.out.println(a>b);
       System.out.println(a<b);
       System.out.println(a==b);
       System.out.println(b%a);
       System.out.println(b/a);
  }


}

注意点:%取余数 /除

 

4、一元运算符 ++ --

public class Demo04 {
   public static void main(String[] args) {
       //++ -- 自增自减   一元运算符
       int a = 3;
       int b = a++;// a = a+1 先执行代码,在自增
       System.out.println(a);//a=4
       int c = ++a;//a = a+1   先自增,在执行代码

       System.out.println(a);
       System.out.println(b);
       System.out.println(c);
       System.out.println(c);


       //幂运算 2的3次方
       double pow = Math.pow(2,3);//Math.pow幂运算
       System.out.println(pow);
  }
}

注意点:1、b = a++; 先执行将a赋值给b,在a++

2、b = ++a; 先a++,在将a赋值给b

3、幂运算Math.pow(2,3) 2的3次方

 

5、逻辑运算符

public class Demo05 {
   public static void main(String[] args) {
       //与(and) 或(or) 非(取反)

       boolean a = true;
       boolean b = false;

       System.out.println("a && b:"+(a&&b));
       //&与运算符,两真才真
       System.out.println("a||b:"+(a || b));
       //||或运算符,一真即真
       System.out.println("!(a&&b):" +!(a&&b));
       //取反


       int c = 5;
       boolean d = (c<4)&&(c++<4);
       System.out.println(c);
       //由于c<4false后面的(c++<4)不会执行,所以输出c为初始值
       System.out.println(d);

  }
}

注意点:1、&与运算符,两真才真

2、||或运算符,一真即真

3、^取反

6、位运算符

public class Demo06 {
   public static void main(String[] args) {
       /*
       A = 0011 1100
       B = 0000 1101
       ------------------------------
       A&B = 0000 1100
       A|B = 0011 1101
       A^B = 0011 0001 (异或相同为0 不同为1)
       ~B = 1111 0010 (取反)

       左移(*2)右移(/2)
       << >>
        */
       System.out.println(2<<3);//2右移3位
       //前:0000 0010   2
       //后:0001 0000   16
  }
}

 

 

7、简化的二元运算

public class Demo07 {
   public static void main(String[] args) {
       int a = 10;
       int b = 20;

       a+=b;//a=a+b
       a-=b;//a=a-b
       System.out.println(a);

       //字符串连接符 +   , String
       System.out.println(a+b);
       System.out.println(""+a+b);//""字符串在前面,结果则是字符串连接
       System.out.println(a+b+"");//""字符串在后面,计算a+b
  }
}

 

8、条件运算符

public class Demo08 {
   public static void main(String[] args) {
       // x ? y : z
       //如果x==true , 则结果为y,否则结果为z

       int score = 80;
       String type = score < 60 ? "不及格":"及格";
       // if score<60则不及格,反之则及格
       System.out.println(type );
  }
}
 

标签:int,System,运算符,println,public,out
From: https://www.cnblogs.com/ctynlnlnl/p/16790809.html

相关文章