[Java Foundation Series] 3 - operators and expressions of Java

1. Operator

1.1 arithmetic operators

  • Arithmetic operator: two operands are evaluated

    Operatordescribe
    +Addition and summation
    -Subtraction and subtraction
    *Multiplication and quadrature
    /Division and quotient
    %Module and remainder
  • Arithmetic operator: unary operator (only one operand)

    Operatordescribe
    ++Increment, variable value + 1
    Decrement, variable value - 1

for instance:

public class TestOperation1{
	public static void main(String[] args){
		int a = 10;
		int b = 3;
		
		System.out.println(a / b); // Quotient = 3
		
		System.out.println(a % b); // Remainder = 1
		
		double d = 10.0;
		
		int c = 3;
		
		System.out.println(d / c); // Quotient 3.33
		
		int num1 = 10;
		
		num1++; //Self increment 1
		
		System.out.println(num1);
        
        int num2 = 10;
		
		num2--; //Self subtraction 1
		
		System.out.println(num2);
		
		int num3 = 5;
		
		// priority
	
		// First + +: first + +, and then print the self incremented value
		
		// After + +: print the current value first, and then++
		System.out.println(num3++); 
		
		//System.out.println(++num3);
		
		System.out.println(num3);
		
		int num4 = 100;
		
		// First + +: first + +, then assignment
		
		// After + +: assign values first, and then++
		
		int num5 = num4++;
		
		System.out.println(num5);
		
		System.out.println(num4);
		
		
		int num6 = 100;
		
		// When used alone, there is no difference in priority
		
		num6++; //Self increment 1
		
		System.out.println(num6);
	}
}

The execution results are as follows:

1.2 assignment operator

  • Assignment operator: assignment from the right side of the equal sign to the left side of the equal sign
Operatordescribe
=Direct assignment
+=Assignment after summation
-=Assignment after difference
*=Assignment after quadrature
/=Assignment after quotient
%=Assignment after remainder

for instance:

public class TestOperation2{
	public static void main(String[] args){
	
	int a = 10; // Assignment Operators 
	
	a += 5; // On the basis of a + 5 > > > A = a + 5;
	
	System.out.println(a);
	
	int b = 20;
	
	b -= 3; // b = b - 3;
	
	System.out.println(b);
	
	int c = 30;
	
	c %= 4; // c = c % 4;
	
	System.out.println(c);
	}
}

The execution results are as follows:

1.3 relational operators

  • Relational operators: comparing two operands
Operatordescribe
>greater than
<less than
>=Greater than or equal to
<=Less than or equal to
==be equal to
!=Not equal to

for instance:

public class TestOperation3{
	public static void main(String[] args){
		
		int a = 10;
		int b = 6;
		
		System.out.println(a > b);
		System.out.println(a < b);
		
		System.out.println(a >= b);
		System.out.println(a <= b);
		
		System.out.println(a == b);
		System.out.println(a != b);
	}
}

The execution results are as follows:

1.4 logical operators

  • Logical operator: logical comparison between operands or expressions of two boolean types

    Operatorsemanticsdescribe
    &&And (and)Both operands are true at the same time, and the result is true
    ||Or (or)Two operands, one of which is true and the result is true
    !Non (reverse)True is false, false is true

    for instance:

    public class TestOperation4{
    	public static void main(String[] args){
    		
    		int javaScore = 100;
    		
    		int webScore = 99;
    		
    		// Compare whether they are equal
    		System.out.println(javaScore == webScore);
    		
    		// Judge whether both are full marks
    		
    		System.out.println(javaScore == 100);
    		System.out.println(webScore == 100);
    		
    		// Through an expression, judge whether both are full marks
    		
    		// Two expressions are true > > > true at the same time. If one of the two expressions is false > > > false
    		System.out.println(javaScore == 100 && webScore == 100);
    		
    		
    		// Judge whether there is a full score at one time
    		// One is true and the result is true
    		System.out.println(javaScore == 100 || webScore == 100);
    		
    		
    		boolean result = javaScore == 100;
    		
    		// Is Java a full mark?
    		System.out.println(result); // Result > > > true
    		
    		// Isn't Java a full mark?
    		System.out.println(!result); // Result > > > false
    	}
    	
    }
    

    The execution results are as follows:

1.5 ternary operator

  • Ternary operator: assign the judged result to the variable

    Operatorsemanticsdescribe
    ?:Boolean expression? Result 1: result 2When the expression result is true, the result 1 is obtained; When the expression result is false, the result 2 is obtained

For example:

public class TestOperation5{
	public static void main(String[] args){
		
		// 1. Judge
		// 2. Assignment
		//Syntax: Boolean expression? Value 1: value 2
		
		int javaScore = 100;
		
		String result = javaScore == 100 ? "congratulations" : "come on.";
		
		System.out.println(result);
		
		int webScore = 99;
		
		int result2 = webScore == 100?666:111;
		
		System.out.println(result2);
		
	}
	
}

The execution results are as follows:

2. Expression

  • Use operators to connect variables or literals, and you can get a final result

    For example:

    1 + 2 + 3;
    int a = 2; a - 1;
    int b = 10; int c = 20; b * c; c / b;
    short d = 100; int e = 200; d > e; d <= e;
    
    
    

Tags: Java

Posted on Sun, 12 Sep 2021 03:14:18 -0400 by activomate