Java基础05:基本运算符

时间:2021-09-01
本文章向大家介绍Java基础05:基本运算符,主要包括Java基础05:基本运算符使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

运算符

Java语言支持如下运算符:

  • 算术运算符:+,-,*,/,%,++,--
  • 赋值运算符:=
  • 关系运算符:>,<,>=,<=,==,!=instance of
  • 逻辑运算符:&&,||,!
  • 位运算符:&,|,^,~,>>,<<,>>>(了解)
  • 条件运算符:?:
  • 扩展赋值运算符:+=,-=,*=,/=

算数运算,相加

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

        System.out.println(a+b+c+d); //long  输出123123264
        System.out.println(b+c+d);   //int   输出141
        System.out.println(c+d);     //int   输出18
    }
}

关系运算,取余

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

        //取余,模运算
        System.out.println(c%a);// 21/10=2······1

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

自增,自减

public class Demo04 {
    public static void main(String[] args) {
        //一元运算符
        //++  --  自增,自减
        int a=3;

        //a++先赋值再自增
        //++a先自增再赋值
        int b=a++;
        int c=++a;

        System.out.println(a);//输出5
        System.out.println(b);//输出3
        System.out.println(c);//输出5

        //幂运算 2^3  2*2*2=8  很多运算,我们会运用一些工具类来操作
        double pow= Math.pow(2,3);
        System.out.println(pow);
    }
}

逻辑运算

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

        System.out.println("a&&b:"+(a&&b));//逻辑与运算,2个为真,才是真
        System.out.println("a||b:"+(a||b));//逻辑或运算:1个为真,就是真
        System.out.println("!(a&&b):"+!(a&&b));//真变假,假变真

        //短路运算
        int c=5;
        boolean d=(c<4)&&(c++<4);//前面短路,后面不走了
        System.out.println(d);
        System.out.println(c);
    }
}

位运算

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*8=16   2*2*2*2   效率极高
        //<<   *2
        //>>   /2

        System.out.println(2<<3);
    }
}

扩展赋值运算符

public class Demo07 {
    public static void main(String[] args) {
        int a=10;
        int b=10;
        a+=b;//a=a+b
        System.out.println(a); //输出20

        //字符串连接符  +  String
        //只要+左右有一个String类型,“”在前面,进行拼接
        //                       “”在后面,进行计算
        System.out.println(a+b);     //输出30
        System.out.println(""+a+b);  //输出2010
        System.out.println(a+b+"");  //输出30
    }
}

三元运算符

//三元运算符
public class Demo08 {
    public static void main(String[] args) {
        //x ? y : z
        //如果x等于true,则结果为y,否则为z
        int score=80;
        String type=  score<60?"不及格":"及格";
        System.out.println(type);
    }
}

原文地址:https://www.cnblogs.com/Siri-99/p/15215528.html