Java基本运算符

2022/6/6 1:23:08

本文主要是介绍Java基本运算符,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

基本运算符

Java语言支持以下运算符:

  • 算术运算符:+ , - , * ,/ , %(取余,模运算),++ , --
  • 赋值运算符:= (int a = 10;)=>把10赋值给a
  • 关系运算符:> , < , >= , <= , ==(等于), !=(不等于), instanceof
  • 逻辑运算符:&&(与), ||(或) , !(非)
  • 位运算符:& , | , ^ , ~ ,>> , << , >>>
  • 条件运算符: ?, :,
  • 拓展赋值运算符:+= ,-=,*=,/=

算术运算符:

//算术运算符
//除以小数时,需要注意作用范围
public class Demo01 {
    public static void main(String[] args) {
        //二元运算符
        int a= 10;
        int b= 20;
        int c= 30;
        int d= 40;
        int e = 11;

        System.out.println(a+b);
        System.out.println(a-b);
        System.out.println(a*b);
        System.out.println(a/(double)b);
        System.out.println(e%a); //取余,模运算
    }
}
//输出的类型
public class Demo02 {
    public static void main(String[] args) {

        long a = 123456L;
        int b = 123;
        short c = 12;
        byte d = 2;

        System.out.println(a+b+c+d); //Long
        System.out.println(b+c+d);  //int
        System.out.println(c+d);    //int
        System.out.println(d);      //byte
    }
}
//一元运算符
public class Demo04 {
    public static void main(String[] args) {
        // ++  -- 自增 自减  一元运算符

        int a = 3;
        int b = a++; //先赋值,再自增 ;a = a + 1
        int c = ++a; //先自增,再赋值
        int d = c--; //先赋值,再自减
        int e = --c; //先自减,再赋值

        System.out.println(a); //5
        System.out.println(b); //3
        System.out.println(c); //3
        System.out.println(d); //5
        System.out.println(e); //3
   }
 }

关系运算符:

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

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

幂运算:

//幂运算 2*2*2=8  使用工具类
public class Demo04 {
    public static void main(String[] args) {
        double pow = Math.pow(3, 2);
        System.out.println(pow); //9.0
    }
}

逻辑运算符:

//逻辑运算符
public class Demo05 {
    public static void main(String[] args) {
       //与(and) 或(or) 非(取反)
       boolean a = true;
       boolean b = false;

        System.out.println("a&&b:" + (a&&b)); //两个变量为真,则结果才为true
        System.out.println("a||b:" + (a||b)); //两个变量有一个为真,则结果才为true
        System.out.println("!(a$$b):" + !(a&&b)); //如果是真则为假,如果是假则为真

        //短路运算
        int c = 5;
        boolean d = (c<4)&&(c++<4); //如果前面条件为false,则不执行后面的条件
        System.out.println(d); //false
        System.out.println(c); //结果应为6,实则输出为5
    }
}

位运算符:

//位运算符
public class Demo06 {
    public static void main(String[] args) {
      /*
           A = 0011 1100
           B = 0000 1101
    ----------------------------------------
    A&B(A与B) = 0000 1100  =>两个是1才为1,否则为0
    A|B (A或B) = 0011 1101  =>两个是0才为0,否则为1
    A^B (异或) = 0011 0001  =>两个相同为0,否则为1
    ~ B (取反) = 1111 0010  =>与B相反

    2*8 = 16 =>如何最快计算
    效率高
    <<  左移  *2
    >>  右移  /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);

    }
}

拓展赋值运算符:

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);

        //字符串连接符
        System.out.println(""+a+b); //1020;如果字符串在前面,后面的则拼接
        System.out.println(a+b+""); //30;如果字符串在后面,前面的则继续运算
    }
}

条件运算符:

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

        int money = 80;
        String type = money <60 ? "恋爱的酸臭" : "单身狗的清香";
        System.out.println(type);
    }
}


这篇关于Java基本运算符的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程