Basic Java syntax - operators

Posted by Dj Kat on Mon, 27 Dec 2021 19:07:03 +0100

operator

1. General

  • Operator: a symbol that operates on constants or variables
  • Expression: an expression that connects constants or variables with operators, which conforms to java syntax
int a = b + c;
// +Yes operator b + c is an expression

2. Arithmetic operators

2.1 Symbols & functions

Symboleffect
+plus
-reduce
*ride
/place
%Surplus

2.2 precautions

  • When an arithmetic expression contains values of different basic data types, the type of the entire arithmetic expression will be automatically promoted

  • char type participates in data arithmetic operation. It uses the decimal value corresponding to the bottom of the computer. You need to remember the value corresponding to three characters

    • 'a' - 97: a-z is continuous, so the corresponding value of 'b' is 98 and 'c' is 99, which are added successively
    • 'a' - 65: A-Z is continuous, so the corresponding value of 'B' is 66 and 'C' is 67, which are added successively
    • '0' - 48:0-9 are continuous, so the corresponding value of '1' is 49 and '2' is 50, which are added successively
  • When a string appears in the "+" operation, the "+" is a * * character connector * *, not an arithmetic operator

    System.out.println("123" + 1) //Output 1231
    // "" directly j before ""
    System.out.println("123" + 2 + 3)//Output 12323
    // The "" is followed by a 2 + 3 operation, and then spliced with the string
    System.out.println(2 + 3 + "123");//Output 5132
    

2.3 code examples

public class Demo01 {
    public static void main(String[] args) {
        // Binary operator
        int a = 10;
        int b = 20;
        int c = 25;
        int d = 25;

        System.out.println(a+b); // 30
        System.out.println(a-b); // -10
        System.out.println(a*b); // 200
        /*Because both a and b are int types, the result calculated during division is still int type,
        Precision will be lost. If type conversion is not performed here, the output result will be 0 */
        System.out.println(a/(double)b); // 0.5 pay attention to division or generate decimals
        System.out.println(c%a); // 5 modular operation remainder
    }
}
public class Demo02 {
    public static void main(String[] args) {
    //When the arithmetic expression contains values of different basic data types, the type of the whole arithmetic expression will be automatically promoted ★
        long a = 123456456465L;
        int b = 123;
        short c = 10;
        byte d = 8;

        /*
        abcd In addition, a is a Long type with a larger capacity than other types,
        Therefore, the output result is also of type Long. Similarly
         */
        System.out.println(a+b+c+d); //12345645606 long type
        System.out.println(b+c+d); //141 Int type
        System.out.println(c+d); //18 Int type
    }
}

3. Assignment operator

3.1 Symbols & functions

Symboleffect
=assignmenta = b assigns the value on the right of "=" to the left
+=Assignment after additiona += b --> a = a + b
-=Assignment after subtractiona -= b --> a = a - b
*=Assignment after multiplicationa *= b --> a = a * b
/=Assignment after divisiona /= b --> a = a / b
%=Assignment after remaindera %= b --> a = a % b

3.2 precautions

  • "=" the left side must be modifiable and cannot be a constant

  • All operators except "=" are extended operators

  • The extended operator implies a cast

    short a = 1;
    //a = a +10;  An error is reported, because 10 is an integer, the default is int, and the operation result is int. assigning to short may lose precision
    a += 10; //No error will be reported because the cast is implied, which is equivalent to a = (short)(a+10)
    

4. Self increasing and self decreasing operator

4.1 symbol & Function & Description

Symboleffectexplain
++Self increasingAdd one to the value of the variable
Self subtractionThe value of the variable minus one

4.2 precautions

  • The symbol can be either after or before the variable

    a++     or ++a
    
  • When used alone, the result of the symbol before and after the variable is the same

  • When participating in the operation

    • a + + takes variables to participate in the operation first, and then++
    • ++a, i.e. + +, and then take variables to participate in the operation

4.3 code examples

Use alone
int a = 10;
a++;
System.out.println(a); // 11
//Participate in operation
public class Demo04 {
    public static void main(String[] args) {
        // ++Self increasing -- self decreasing unary operator
        int a = 3;

        int b = a++; //After executing this line of code, assign a value to b first, and then increase it automatically
        // a++  a = a + 1

        System.out.println(a); // 4
        int c = ++a; // First line a = a + 1, and assign the value of a to c after operation

        System.out.println(a); // 5
        System.out.println(b); // 3
        System.out.println(c); // 5

        //Power operation 2 ^ 3 2 * 2 * 2 = 8 many operations will use some tool classes to operate!
        double pow = Math.pow(2,3);
        System.out.println(pow); //8.0
    }
}

5. Relational operators

5.1 Symbols & descriptions

Symbolexplain
==a==b determines whether the values of a and b are equal. It is true if it is true and false if it is not
!=a!= b determines whether the values of a and b are not equal. It is true if it is true and false if it is not
>a> b determines whether the value of a is greater than b, which is true and false
>=a> = b determines whether the value of a is greater than or equal to b, which is true and false
<If a < b, judge whether the value of a is less than b. if it is true, it is false
<=A < = b determines whether the value of a is less than or equal to b, which is true and false

5.2 precautions

  • The results of relational operators are boolean, and there are only two results, true and false
  • "=" and "= =" are two operators with different functions. One is assignment and the other is to judge whether they are equal

5.3 code examples

public class Demo03 {
    public static void main(String[] args) {
        //Results returned by relational operators: correct, wrong, Boolean
        //Often used with if

        int a = 10;
        int b = 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
    }
}

6. Logical operator & short circuit logical operator

6.1 general

  • Logical operators connect the relational expressions of various operations to form a complex logical expression to judge whether the expression in the program is true and false

6.2 logical operators

6.2. 1 Symbol & Function & Description

Symboleffectexplain
&Logic andA & b: both a and b are true, and the result is true, otherwise false < false is false >
|Logical ora|b: both a and B are false, and the result is false, otherwise true < true is true >
^Logical XORa^b: different results of a and B are true, and the same is false
!Logical non! a: The result is opposite to that of A

6.2. 2 code example

public class Demo05 {
    public static void main(String[] args) {
        // And (and) or (or) not (negative)
        boolean a = true;
        boolean b = false;
        
        System.out.println("a&b:"+(a & b)); //False if a value is false, the result is false
        System.out.println("a|b:"+(a | b)); //True has a value of true, and the result is true
        System.out.println("!a:"+(!a)); //false the result is the opposite
    }
}

6.3 short circuit logic operator

6.3. 1 Symbols & functions

Symboleffect
&&Short circuit and
||Short circuit or

6.3. 2 code example

public class Demo05 {
    public static void main(String[] args) {
        int a = 1;
        int b = 2;

        System.out.println((a>b) && (a<b)); //False because a > b is false, a < B will not run, and the judgment result is returned directly as false
        System.out.println((a<b) || (a>b)); //True because a < B is true, a > B will not run, and the judgment result true will be returned directly
    }
}

6.4 differences

  • The & & and | operators are usually used in the process of writing code
&(| similarly)&&(| similarly)
Whether the expression on the left is false or not, the expression on the right must be evaluatedIf the expression on the left is false, the expression on the right will not operate and will directly return the judgment result false

7. Ternary operator

7.1 syntax

Relational expression ? Expression 1 : Expression 2

7.2 description

  • If the relationship expression determines that the result is true, the expression 1 is called
  • If the relationship expression determines that the result is false, expression 2 is called

7.3 code examples

public class Demo {
    public static void main(String[] args) {
       int a = 1;
       int b = 2;
       //  x ? y : z
       //If x==true, the result is y; otherwise, the result is z
       int c = a>b ? a+b : b-a; //a> B -- false b-a
       System.out.println(c);

        int d = a<b ? a+b : b-a;
        System.out.println(d); //A < B -- true a+b
    }
}
public class Demo08 {
    public static void main(String[] args) {
        //  x ? y : z
        //If x==true, the result is y; otherwise, the result is z

        int score = 50;
        String type = score < 60 ?"fail,":"pass";
        System.out.println(type); // fail,
    }
}

8. Binary operation < code example >

//Bit operation
public class Demo06 {
    public static void main(String[] args) {
        /*
        A = 0011 1100
        B = 0000 1101

      A&B = 0000 1100 0 if there is 0
      A|B = 0011 1101 If there is 1, it is 1
      A^B = 0011 0001 The same is 0, the difference is 1
       -B = 1111 0010

    2*8 How to calculate the fastest
    2*8 = 16 2*2*2*2
    Extremely efficient
    <<  Shift left * 2
    >>  Shift right / 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<<3); //16

    }
}

Topics: Java