自增自减,幂运算,逻辑运算符、短路运算,位运算,字符串连接符,三元运算符复习

package com.kun.base;

public class Demo02 {
    public static void main(String[] args) {
        int a = 5;
        int b = a--;//执行完这行代码前先赋值b再自减
        System.out.println(a);//4 执行完自减后输出的a
        int c = --a;//执行完这行代码前先自减再复制c
        System.out.println(a);//3
        System.out.println(b);//5
        System.out.println(c);//3

        System.out.println("==========================");

        double pow = Math.pow(4,2);//幂运算 2^4 2*2*2*2=16
        System.out.println(pow);

        System.out.println("==========================");

        //逻辑运算符
        boolean a1 = true;
        boolean b1 = false;
        System.out.println("a1 && b1:"+(a1&&b1));//逻辑与运算:两个变量都为真,结果才为true
        System.out.println("a1 || b1:"+(a1||b1));//逻辑或运算:两个变量有一个为真,则结果才为true
        System.out.println("!(b1 && a1):"+!(b1&&a1));//如果为真,则变为假,如果是假则变为真

        //短路运算
        int c1 = 6;
        boolean d1 = (c1<5)&&(c1++<5);//c1<5判定为false 直接跳过c1++<5
        System.out.println(d1);//false
        System.out.println(c1);//6未执行c1++(短路运算)
        int c2 = 6;
        boolean d2 = (c2>5)&&(c2++<5);//c1>5判定为true 继续执行c2++<5判定为false 输出结果为false
        System.out.println(d2);//false
        System.out.println(c2);//7执行完c2++后输出c2

        System.out.println("==========================");

        /*位运算
        A = 0011 1100
        B = 0000 1101
        --------------------------------
        A&B = 0000 1100 有0为0无0为1
        A|B = 0011 1101 有1为1无1为0
        A^B = 0011 0001 相同为0反之为1
        ~B  = 1111 0010 取反

        2*8 = 16   2*2*2*2
        效率极高!!!
        <<    *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<<4);//2*2*2*2*2
        System.out.println(2>>1);//2/1

        System.out.println("==========================");

        int a2 = 1;
        int b2 = 2;
        //字符串连接符 + ,String类
        System.out.println(a2+b2);
        System.out.println(""+a2+b2);//此处“""”为字符串所有“+”为字符串连接符
        System.out.println(a2+b2+"2");//现将a2+b2求和再连接字符串“2”

        System.out.println("==========================");

        //三元运算符
        // x ? y : z (如果x==true,则结果为y,否则结果为z)
        int score1 =50;
        int score2 =70;
        String type1 = score1 <60 ?"不合格":"合格";
        System.out.println("1号成绩"+type1);
        String type2 = score2 <60 ?"不合格":"合格";
        System.out.println("2号成绩"+type2);
    }
}
posted on 2025-10-22 08:32  Sea(kun)  阅读(3)  评论(0)    收藏  举报