Java学习2 基础3 语法5(运算符)

Java学习2 基础3 语法5(运算符)

一、重要

1、算数运算符

+ - * / % ++ --

 

Ⅰ、+-*/%

public class Demo235 {
   public static void main(String[] args) {
       int a=10;
       int b=20;
       System.out.println(a+b);   //加法运算
       System.out.println(a-b);   //减法运算
       System.out.println(a*b);   //乘法运算
       System.out.println(a/b);   //除法运算,此时结果为0
       System.out.println(a/(double)b);   //除法运算,此时结果为0.5
       System.out.println(a%b);   //取余数
       System.out.println(b%a);   //取余数
  }
}

30 -10 200 0 0.5 10 0

Process finished with exit code 0

注:

a+b+c+d:有long时,结果为long;有double时,结果为double;优先级高于int的都没有时,无论其中有没有int,结果都为int。

public class Demo235 {
   public static void main(String[] args) {
       double a = 3.1415926;
       long b = 31415926L;
       int c = 314;
       short d = 10;
       byte e = 1;
       System.out.println(a+b+c+d+e);   //3.14162541415926E7
       System.out.println(b+c+d+e);   //31416251
       System.out.println(c+d+e);   //325
       System.out.println(d+e);   //11
  }
}

 

3.14162541415926E7 31416251 325 11

Process finished with exit code 0

 

Ⅱ、++、--

public class Demo235 {
   public static void main(String[] args) {
       int a = 10;
       int b = a++;   //此时,等价于 int b=a;a=a+1;
       System.out.println(a);   //a==11
       System.out.println(b);   //b==10
       int c = ++a;   //此时,等价于 a=a+1;int c=a
       System.out.println(a);   //a==12
       System.out.println(c);   //c==12
  }
}

 

11 10 12 12

Process finished with exit code 0

 

2、赋值运算符

=

 

public class Demo235 {
   public static void main(String[] args) {
       int a = 10;
       int b;
       b=a;
       System.out.println(b);
  }
}

 

10

Process finished with exit code 0

3、关系运算符

> , < , >= , <= , == , != , instanceof

 

public class Demo235 {
   public static void main(String[] args) {
       int a=10;
       int b=20;
       int c=20;
       System.out.println(a>b);   //   false
       System.out.println(a<b);   //   true
       System.out.println(a>=b);   //   false
       System.out.println(a<=b);   //   true
       System.out.println(a==b);   //   false
       System.out.println(a!=b);   //   true
       System.out.println(b>c);   //   false
       System.out.println(b<c);   //   false
       System.out.println(b>=c);   //   true
       System.out.println(b<=c);   //   true
       System.out.println(b==c);   //   true
       System.out.println(b!=c);   //   false
  }
}

 

false true false true false true false false true true true false

Process finished with exit code 0

 

4、逻辑运算符

&& , || , !

 

public class Demo235 {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        System.out.println(a<b&&a!=0);   //   true
        System.out.println(a<b&&a==0);   //   false
        System.out.println(a<b||a!=0);   //   true
        System.out.println(a<b||a==0);   //   true
        System.out.println(a==b||a==0);   //   false
        System.out.println(!(a==0));   //   true
        System.out.println(!(a!=0));   //   false
    }
}

 

true false true true false true false

Process finished with exit code 0

 

拓展:Math工具类

public class Demo235 {
    public static void main(String[] args) {
        double a = 3;
        double b = Math.pow(a,3);
        System.out.println(b);
    }
}

 

27.0

Process finished with exit code 0

二、次要

1、位运算符

& , | , ^ , ~ , >> , << , >>>

A: 0011 1100

B: 0000 1101

A&B: 0000 1100

A|B: 0011 1101

A^B: 0011 0001 异或:同位置数相同则为0,不同则为1

~B: 1111 0010 按位取反

 

右移、左移(>>、<<)

A: 0000 0010

右移>>: 0000 0001 左移导致/2

左移<<: 00000100 右移导致*2

public class Demo235 {
    public static void main(String[] args) {
        int a = 2;
        System.out.println(a>>1);   //==1
        System.out.println(a<<1);   //==4
        System.out.println(a<<2);   //==8
    }
}

注:

  1. 右移、左移不能对浮点数使用,例子中如果定义的是 duoble a = 3.14 会报错;

  2. 注意5/2=2、3/2=1、1/2=0等情况。

 

2、条件运算符

?:

x ? y : z

x为真则结果为y,否则为z

public class Demo235 {
    public static void main(String[] args) {
        int a = 10;
        int b;
        int c;
        b = (a<5)?666:888;
        c = (a>=5)?666:888;
        System.out.println(b);   //b==888
        System.out.println(c);   //c==666
    }
}

888 666

Process finished with exit code 0

 

3、扩展赋值运算符

+= , -= , *= , /=

a+=b 等价于 a=a+b

a-=b 等价于 a=a-b

public class Demo235 {
    public static void main(String[] args) {
        int a =  10;
        int b = 20;
        a+=b;
        System.out.println(a);   //10+20=30
        a-=b;
        System.out.println(a);   //30-20=10
        a*=b;
        System.out.println(a);   //10*20=200
        a/=b;
        System.out.println(a);   //200/20=10
    }
}

30 10 200 10

Process finished with exit code 0

拓展

public class Demo235 {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        System.out.println(""+a+b);   //1020  字符在左,后两个拼接,再和字符拼接
        System.out.println(a+b+"");   //30   字符在右,后两个相加,再和字符拼接
        System.out.println("x"+a+b);   //x1020
        System.out.println(a+b+"x");   //30x
    }
}

1020 30 x1020 30x

Process finished with exit code 0

补充 :

一、优先级

优先级运算符结合性
1 ()、[]、{} 从左向右
2 !、+、-、~、++、-- 从右向左
3 *、/、% 从左向右
4 +、- 从左向右
5 <<、>>、>>> 从左向右
6 <、<=、>、>=、instanceof 从左向右
7 ==、!= 从左向右
8 & 从左向右
9 ^ 从左向右
10 | 从左向右
11 && 从左向右
12 || 从左向右
13 ? : 从右向左
14 =、+=、-=、*=、/=、&=、|=、^=、~=、«=、»=、>>>= 从右向左
posted @ 2022-02-23 22:43  雷电法王沃利贝尔  阅读(105)  评论(0)    收藏  举报