Java教程

运算符学习笔记

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

运算符

  • 算术运算符:+,-,*,/,% 取余 模运算,++,--

  • 赋值运算符:=

  • 关系运算符:>, <, >=, <=, ==, !=不等于, instanceof

  • 逻辑运算符:&&,||,! //与或非

  • 位运算符:&,|,^,~,>>, <<, >>>(了解)

  • 条件运算符? :

  • 扩展赋值运算符:+=,-=,*=,/=

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(a/(double)b);//0.5 int整形 强制转换
    }
}
输出:

-10

0.5
public class Demo02 {
    public static void main(String[] args) {
        long a = 1234561223122L;
        int b = 123;
        short c = 10;
        byte d = 8;
        System.out.println(a+b+c+d);//long
        //两个或多个操作时有一个数为long那么结果也为long
        //如果没有long 则为int类型
        System.out.println(b+c+d);//int
        System.out.println((c+d));//int
    }
}
输出:

    18
public class Demo03 {
    public static void main(String[] args) {
        //关系运算符返回结果:正确  错误  布尔值
​
        int a = 10;
        int b = 20;
        int c = 21;
        System.out.println(c%a);//取余数  c/a 21/10=2...1
        System.out.println(a>b);
        System.out.println(a<b);
        System.out.println(a==b);
    }
}
输出:

false
true
false
public class Demo04 {
    public static void main(String[] args) {
        //++  --  自增  自减  一元运算符
        int a = 3;
        int b = a++;//a++  a=a+1  先赋值,再自增
        System.out.println(a);
        int c = ++a;//++a  a=a+1  先自增,再赋值
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
​
        //幂运算2^3 2*2*2=8 很多运算  我们会使用工具类来操作
        double pow = Math.pow(2,3);
        System.out.println(pow);
    }
}
输出:


    5
    8.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);//当执行到(c<4)已完成判断 终止后续操作
        System.out.println(d);
        System.out.println(c);
    }
}
输出:

    a&&b:false
    a||b:true
    !(a&&b:)true
    false
    524
public class Demo06 {
    public static void main(String[] args) {
      /*
      A = 0011 1100
      B = 0000 1101
    ----------------------------------
      A&B = 0000 1100 两个都是1为1
      A|B = 0011 1101 其中一个为1则为1
      A^B = 0011 0001 同0异1
      ~B = 1111 0010  取反
​
      2*8 = 16  2*2*2*2
      << *2
      >> /2
​
        0
        1
        2
        3
        4
        8
        13
       */
        System.out.println(2<<3);//2*2*2*2
        System.out.println(3<<3);//3*2*2*2
    }
}
输出:

    24
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);
        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);
    }
}
输出:
    及格

 

 

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