Java运算符

六.运算符

  1. 算数运算符:+、-、*、/、%、++、--
//算术运算符
public class Demo01 {
    public static void main(String[] args) {
        /*  二元运算符 "/ , %"
        9除以2=4....1
         */
        System.out.println(9/2);    //4
        System.out.println(9%2);    //1
        System.out.println("----------------");
        //自增++、自减--,一元运算符
        int a=1;
        System.out.println(a);  //a==1
        System.out.println("----------------");
        int b=a++;  //b=a=1,a=a+1=2,先赋值,后运算
        System.out.println(a);  //2
        System.out.println(b);  //1
        System.out.println("----------------");
        int c=++a;  //a=a+1=3,c=a=3,先运算,后赋值
        System.out.println(a);  //3
        System.out.println(c);  //3
        System.out.println((c++)+(++a));    //a=a+1=4,c+a=3+4=7

    }
}

image-20210530220358359

  1. 赋值运算符:=

  2. 关系运算符:>, <, >=, <=, ==, !=

  3. 逻辑运算符:&&, ||, !

//逻辑运算符
public class Demo02 {
    public static void main(String[] args) {
        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));  //逻辑非,真变假,假变真
        System.out.println("-----------------");
        //短路运算
        int c = 1;
        boolean d = ((c>2)&&(c++)<6);//逻辑与运算,前面的(c>2)已经为false,不再执行后面的(c++)运算
        System.out.println(c);  //c==1
        System.out.println(d);      //false
        System.out.println("-----------------");
        int c_=1;
        boolean e = ((c_<2)&&(c_++)<6);
        System.out.println(c_);     //c_==2,执行了后面的(c_++)
        System.out.println(e);      //true
    }
}

image-20210530220551279

  1. 位运算符:&, |, ^, ~, >>, <<
//位运算符,与二进制相关
public class Demo04 {
    public static void main(String[] args) {
        /*
        A=0011 1100
        B=0000 1111
        A&B = 0000 1100     //与运算,有0为0
        A|B = 0011 1111     //或运算,有1为1
        A^B = 0011 0011     //异或,相同为0,不同为1
        ~A = 1100 0011
         */
        //位运算符,直接与底层二进制打交道,效率高!!!
        /*
        << 左移1,相当于 *2,左移2,相当于 *2*2
        >>右移1,相当于 /2,右移2,相当于 /2/2
         */
        /*++++++++++面试题:怎样计算2*8更快?
        解析:2*8 = 2*2*2*2 = 2<<3
         */
        System.out.println("2*8="+(2*8));    //16
        System.out.println("2<<3="+(2<<3));   //2*3*3=16
        System.out.println("------------------");
        System.out.println("36/4="+(36/4));   //9
        System.out.println("36>>2"+(36>>2));  //32/2/2=9
        System.out.println("------------------");
        /*+++++++++++++面试题:字符串连接符,“+”
        在“+”号两侧,只要有一侧出现“String”类型,就会将其他操作数转换为“String”类型,再进行连接
        若字符串在操作数前面,则直接进行拼接
        若字符串在操作数后面,则先进行操作数运算,再进行拼接
         */
        System.out.println("a"+10+20);       //a1020
        System.out.println(10+20+"a");      //30a
        System.out.println("a"+(10+20));    //a30
    }
}

image-20210530221559804

  1. 条件运算符:? :
//条件运算符
public class Demo05 {
    public static void main(String[] args) {
        /*
        三元运算符:a ? x : y  //判断a是否为真,为真则为x,为假则为y
         */
        int a=90;
        int b=50;
        String sorce1 = a>60 ? "及格" : "不及格";
        String sorce2 = b>60 ? "及格" : "不及格";
        System.out.println(a+sorce1); //及格
        System.out.println(b+sorce2); //不及格
    }
}

image-20210530221212279

  1. 拓展赋值运算符:+=, -=, *=, /=
posted @   chaplu  阅读(6)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示