运算符,位运算基础,字符串拼接,包机制

简介: 运算符,位运算基础,字符串拼接,包机制运算符优先级尽量使用()• 算术运算符 + - * / % ++ --• 赋值运算符 =• 关系运算符 > < >= <= == !=• 逻辑运算符 && || !• 位运算符 & | ^ ~ >> << >>>• 条件运算符 ? :• 拓展赋值运算符 += -= *= /=

运算符,位运算基础,字符串拼接,包机制


运算符


优先级尽量使用()


  • 算术运算符 + - * / % ++ --


  • 赋值运算符 =


  • 关系运算符 > < >= <= == !=


  • 逻辑运算符 && || !


  • 位运算符 & | ^ ~ >> << >>>


  • 条件运算符 ? :


  • 拓展赋值运算符 += -= *= /=


基本运算符及其运算


Ctrl + d 复制当前行到下一行


public class Demo01 {
    public static void main(String[] args) {
        //二元运算符,两个变量之间计算结果
        //Ctrl + d 复制当前行到下一行
        int a = 10;
        int b = 20;
        int c = 25;
        int d = 25;
        System.out.println(a+b);
        System.out.println(a-b);
        System.out.println(a*b);
        System.out.println((double)a/b);
    }
}


public class Demo02 {
    public static void main(String[] args) {
        long a = 12321312321L;
        int b = 123;
        short c = 10;
        byte d = 8;
        //多种类型数字计算时,有一种为long类型则结果为long类型,否则默认为int类型
        //cast  转换
        System.out.println(a+b+c+d);        //long
        System.out.println(b+c+d);          //int
        System.out.println(c+d);            //int
    }
}


public class Demo03 {
    public static void main(String[] args) {
        //关系运算符     返回结果true    false   布尔值
        //与if配合使用
        int a=10;
        int b=20;
        int c=21;
        //取余,模运算
        System.out.println(c%a);
        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;
        int b=a++;          //a++表示执行完这行代码后,a再自增1,先给b赋值,再自增
        //a++   a = a+1
        System.out.println(a);
        //a++   a = a+1
        int c=++a;          //++a表示执行这行代码前,a先自增1再执行这行代码,先自增再给c赋值
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        //幂运算,使用内置Math函数,很多运算会使用工具类来操作
        double pow=Math.pow(2,3);
        System.out.println(pow);
    }
}


逻辑运算符与位运算符


package operator;
//逻辑运算符
public class Demo05 {
    public static void main(String[] args) {
        //与(and)    或(or)   非(取反)
        boolean a=true;
        boolean b=false;
        System.out.println("a&&b:"+(b&&a));         //逻辑与运算,两者都为真才为真
        System.out.println("a||b:"+(a||b));         //逻辑或运算,两者其一为真就为真
        System.out.println("!(a&&b):"+!(a&&b));     //逻辑非运算,如果是真则为假,如果是假则为真
        //短路运算
        int c=5;
        boolean d=(c<4)&&(c++<4);   //与运算时,前面的值为false,后面的值则不会进行计算和判断
        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^3
        位运算,效率极高,将初始数值转换为二进制,将初始二进制数的所有位置的1在二进制水平上向左或向右移动,来得到最终结果
        <<      *2      左移,初始所有位置的1向左移动
        >>      /2      右移,初始所有位置的1向右移动
         */
        System.out.println(2<<3);     //2<<3与1<<4的结果是相同的
        System.out.println(1<<4);
        System.out.println(3<<3);     //0000 0011    0001 1000   初始值转换为二进制数后,所有位置的1都会按位运算移动
        System.out.println(5<<3);     //0000 0101    0010 1000
        System.out.println(7<<3);     //0000 0111    0011 1000
    }
}


赋值运算符,字符串拼接与三元运算符


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+"");     //字符串在后则会先进行运算再拼接
    }
}


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


包机制


  • 一般利用公司域名倒置作为包名 例如:com.baidu.www


微信图片_20220425162351.png


package com.wyd.operator;
import com.wyd.base.Demo06;         //导入包
import com.wyd.operator.*;          //使用*可导入包下所有的类
import java.util.Date;              //导入包
public class Demo02 {
    public static void main(String[] args) {
        long a = 12321312321L;
        int b = 123;
        short c = 10;
        byte d = 8;
        Date
        //多种类型数字计算时,有一种为long类型则结果为long类型,否则默认为int类型
        //cast  转换
        System.out.println(a+b+c+d);        //long
        System.out.println(b+c+d);          //int
        System.out.println(c+d);            //int
    }
}


相关文章
|
3月前
|
数据处理 Swift
Swift 中的运算符和表达式是构建程序逻辑的基础,包括算术、关系、逻辑、位运算符及赋值运算符,用于数值计算、条件判断、位操作、赋值与更新等
Swift 中的运算符和表达式是构建程序逻辑的基础,包括算术、关系、逻辑、位运算符及赋值运算符,用于数值计算、条件判断、位操作、赋值与更新等。掌握这些工具是编写高效代码的关键。
49 1
|
8月前
|
编译器 C语言
【C语言】:中移位操作符,位操作符详运算规则详解
【C语言】:中移位操作符,位操作符详运算规则详解
68 1
|
9月前
|
编译器 程序员 C语言
【C语言】变长数组,二分查找和数组之间自动替换的实现
【C语言】变长数组,二分查找和数组之间自动替换的实现
|
C语言 C++
【c语言】&& 逻辑运算符运算规则
【c语言】&& 逻辑运算符运算规则
258 0
|
前端开发
形成新数组的方式扩展运算符
形成新数组的方式扩展运算符
50 0
|
存储 编译器 C语言
【C语言】表达式求值相关问题汇总—>隐式类型转换(整型提升)、算数转换与操作符优先级汇总(收藏查阅)
【C语言】表达式求值相关问题汇总—>隐式类型转换(整型提升)、算数转换与操作符优先级汇总(收藏查阅)
126 0
|
存储 算法 编译器
你是真的“C”——操作符详解【下篇】+整形提升+算术转换
详解C语言中操作符+算术转换+整形提升知识点~
198 1
|
前端开发
前端学习案例1-二进制中的操作符
前端学习案例1-二进制中的操作符
78 0
前端学习案例1-二进制中的操作符
|
前端开发
前端学习案例2-二进制中的操作符2
前端学习案例2-二进制中的操作符2
42 0
前端学习案例2-二进制中的操作符2