[Road to Final Review] JAVA (2) D

This is the last part of our JAVA 2, we will update the Java 3 loop statement tomorrow, please stay tuned


Article directory

1. What is an operator

2. Arithmetic operators

Three assignment operators

Four comparison (relational) operators

Five Logical Operators

six bit operator

Seven conditional operators

Octal operator precedence

foreword

In this chapter, we will talk about operators. This is something we must master. Let's take a look!


提示:以下是本篇文章正文内容,下面案例可供参考

1. What is an operator

 Operators are special symbols used to represent data operations, assignments, and comparisons

There are many types of operators, how much do you know?

Classification of operators:

  • Arithmetic operators: +, - ,* ,/ ,%, ++,-- 7
  • Assignment operators: =,+=,-=,*=,%=,/=,>>=,>>>=,<<=,&=,|=,^=
  • Comparison operators, relational operators: >, >=, <, <=, ==, !=
  • Logical operators: &,|,^,!,&&,||
  • Bitwise operators: &,|,^,~,<<,>>,>>>
  • conditional operator: conditional expression
  • Lambda operator: ->

Divided by the number of operands:

  • unary operator
  • binary operator
  • ternary operator

2. Arithmetic operators

1. Basics

operator operation example structure
+ plus sign +3 3
- negative a=3;-a -3
+ add 3+3 6
- reduce 4-3 1
* take 3*4 12
/ remove 3/3 1
% Take the remainder 6%4 2
++

The auto-increment (before) is first calculated and then the value a=2, b=++2

Self-decrement (after) take the value first and then operate a=2, b=2++

a=2,b=++a

a=2,b=a++

a=3,b=3

a=3,b=2

--

Auto-increment (before) calculates value after operation

Self-decrement (after) value first and then operation

a=2,b=--a

a=2,b=a--

a=1,b=1

a=1,b=2

+ string concatenation “he”+“llow” "hello"

Modular example of addition, subtraction, multiplication and division:

public class yunsuan{
    public static void main(String[] args){
        int a = 3;
        int b = 4;

        System.out.println(a+b);//7
        System.out.println(a-b);//-1
        System.out.println(a*b);//12
        System.out.println(a/b);//0
        System.out.println(a%b);//3
        
        System.out.println(5%2);//1
        System.out.println(5%-2);//1
        System.out.println(-5%2);//-1
        System.out.println(-5%-2);//-1
        //商*除数+余数=被除数
        //5%-2  ==>商=-2 余数=1  (-2)*(-2)+1=5             
               }
        }

Two uses of +

1. If both sides are numbers, + means addition

2. If at least one of the two sides is a string, it means splicing

public class yunsuan2 {   
       public static void main(String[] args) {     
            String str1 = "Hello";     
            System.out.println(str1); // Hello
            System.out.println("Hello" + "World"); // HelloWorld
            
            String str2 = "Java";      
            System.out.println(str2 + 520); // Java520     
            System.out.println(str2 + 5 + 20); // Java520   
        }
 }

addition and subtraction operations

public class yunsuan3 {   
        public static void main(String[] args) {     
             int a = 3;        //定义一个变量int
             a++;      
     System.out.println(a);   //像这样无论是++a,还是a++结果都一样,都是4
    }
 }

self-increment and self-subtraction compound operation

    public class ArithmeticTest4 {   
            public static void main(String[] args) {
                int x = 3;  
                int y = x++; 
                //x++的时候,y是4,x是3,当++x的时候,有x是4,y也是4
                System.out.println(x);   
                System.out.println(y);    
                System.out.println("==========");
                int z = 5;     
                //System.out.println(++z);// 输出结果是6,z 的值也是6                     
                System.out.println(z++);// 输出结果是5,z 的值是6   
                System.out.println(z);
  
     } 
}
 

triple assignment operator

symbol =

  • When the data types on both sides of "=" are different, you can use automatic type conversion or use the principle of mandatory type conversion
  • Support for continuous assignment

Extended symbols: +=, -=, *=, /=, %=

operator symbol interpretation
+= The value on the left of the symbol is added to the value on the right, and finally the result is assigned to the variable on the left
-= The value on the left of the symbol is subtracted from the value on the right, and finally the result is assigned to the variable on the left
*= The value on the left of the symbol is multiplied by the value on the right, and finally the result is assigned to the variable on the left
/= The value on the left of the symbol is divided by the value on the right, and finally the result is assigned to the variable on the left
%= The value on the left of the symbol and the value on the right perform a remainder operation, and finally assign the result to the variable on the left
public class fuzhiyunsuna{
    public static void main (String[] args){
        int i1 = 10;  //定义int
        long l1 = i1;  //int升为long类型,自动转化
        byte bb1 = (byte)i1;//强制转化类型,变为int
        int i2 =i1;
        int a1 = 10;
        int b1 = 10;//分别赋值

        int a2,b2;
        a2 = b2 = 10;//连续赋值

        int a3 = 10,b3 = 20;
        
        int m1 = 10;
        m1 += 5;//相当于m=m+5 使用m=15
        System.out.println(m1);
        
        short s2 = 10;//short s2=s2+2
       // s2 = (short)(s2+2);     int类型赋值给short,有损精度
        System.out.println(s2);
        

}
}

Four comparison (relational) operators

operator operation example result
== equal to 4==3 false
!= not equal to 4!=3 true
< less than 4<3 false
> more than the 4>3 true
<= less than or equal to 4<=3 false
>= greater or equal to 4>=3 true
instanceof Check if it is an object of class “Hellow”instanceof String true

  • The results of comparison operators are boolean, either true or false
  • >,>=,<,<=: applicable to basic data types, and only applicable to basic data types
  • ==, !=: apply to basic data types, and reference data types
class CompareTest {   
    public static void main(String[] args) {     
        int i1 = 10;     
        int i2 = 20;
    System.out.println(i1 == i2);//false     
    System.out.println(i1 != i2);//true
    System.out.println(i1 >= i2);//false

        int m = 10;     
        int n = 20;     
    System.out.println(m == n);//false     
    System.out.println(m = n);//20

        boolean b1 = false;     
        boolean b2 = true;     
    System.out.println(b1 == b2);//false         
    System.out.println(b1 = b2);//true  
     } 
}

Distinguish the difference between "=" and "="

Five Logical Operators

a b a&b a&&b a|b a||b !a a^b
true true true true true true false false
true false false false true true false true
false true false false true true true true
false false false false false false true false

Logical operators operate on variables or constants of type boolean, and the result of the operation is also a value of type boolean.

  1. Operator description:
 & and &&: represent the "and" relationship. When the Boolean values ​​on the left and right sides of the symbol are both true, the result can be true. Otherwise, false

         | and || : Indicates an "or" relationship. When one of the Boolean values ​​on both sides of the symbol is true, the result is true. When both sides are false, the result is false

         ! : Indicates a "not" relationship, when the variable Boolean value is true, the result is false. The result is true when the variable Boolean value is false.

         ^ :当符号左右两边布尔值不同时,结果为 true。当两边布尔值相同时,结果为 false。

 区分“&”和“&&”:
相同点:如果符号左边是 true,则二者都执行符号右边的操作

不同点:& : 如果符号左边是 false,则继续执行符号右边的操作
             &&  :如果符号左边是 false,则不再继续执行符号右边的操作
                建议:开发中,推荐使用  &&
区分“|”和“||”:
                 相同点:如果符号左边是 false,则二者都执行符号右边的操作
                 不同点:| : 如果符号左边是 true,则继续执行符号右边的操作
                                ||  :如果符号左边是 true,则不再继续执行符号右边的操作
                建议:开发中,推荐使用  ||

六 位运算符

运算符 运算 范例
<< 左移 3<<2 =12  - 3*2*2=12
>> 右移 3>>1 = 1  - 3/2=1
>>> 无符号右移

3>>>1 = 1 - 3/1=1

& 与运算 6&3=2
| 或运算 6|3=7
^ 异或运算 6^3=5
~ 取反运算 ~6=-7

七 条件运算符

 (条件表达式)?

                表达式1

                表达式2                

条件表达式都是boolean类型的结果,根据boolean的值选择表达式1或表达式2

八 运算符优先级

优先级 运算符说明 Java运算符
1 括号 (),[],{}
2 正负号

+,-

3 单位运算符 ++,--,~,!
4 乘法除法,求余 *,/,%
5 加减法 +,-
6 移位运算符 <<,>>,>>>
7 关系运算符 <,<=,>,>=,instanceof
8 等价运算法 ==,!=
9 按位与 &
10 按位异或 ^
11 按位或 |
12 条件与 &&
13 条件或 ||
14 三位运算符 ?;
15 赋值运算符 =,+=,-=,*=,/=,%=
16 位赋值运算符 &=,|=,<<=,>>=,>>>=

因为优先级太多,记忆起来也麻烦,使用没有必要记住,我们习惯性的用()来控制表达式的执行顺序,不要写的过于复杂,这样只会让我们更难受


总结

提示:这里对文章进行总结:

例如:以上就是今天要讲的内容,本文仅仅运算符使用,这就是我们的第二部分,我分为4部分写的,大家可以关注或者订阅,这样方便大家阅读,相信大家也不容易,我们的Java 三,将会讲循环语句,Java 四会讲到数组,等等一系列,希望大家多多支持,谢谢大家,爱你们呦!

Guess you like

Origin blog.csdn.net/m0_69520030/article/details/130484852
Recommended