# 03JAVA foundation --- knowledge system

catalogue

operator

1. General

2. Operator quick look-up table

3. Classic exercises

3.1 seeking normal and leap years

3.2 self increasing and self decreasing

3.3 compare the size of two numbers

3.4 compare the size of three numbers

4. Expansion

4.1 self increasing and self decreasing operators of arithmetic operators

4.2 logical operators

4.3 priority control

# operator

## 1. General

• Operator is used to concatenate operands of an expression and perform operations on operands.

For example, the expression num1+num2 has operands num1 and num2, and the operator is "+".

• In java language, operators can be divided into five types: arithmetic operator, assignment operator, relational operator, logical operator and bit operator.
• According to different operands, operators are divided into monocular operators, binocular operators and ternary operators.
• A unary operator has only one operand, a binocular operator has two operands, and a ternary operator has three operands.
• Bit operator involves binary bit operation, which is not widely used in java programs.

## 3. Classic exercises

### 3.1 seeking normal and leap years

Demand: accept the year entered by the user to judge whether it is a auspicious year or a normal year

```package cn.tedu;

import java.util.Scanner;

//Accept the year entered by the user to judge whether it is a normal year or a auspicious year
public class Demo02 {
public static void main(String[] args) {
/**
* Demand: receive the year entered by the user and judge whether it is a normal year or a leap year
* If the year is a leap year, you need to meet one of the following two conditions:
* Condition 1: can be divided by 4 and cannot be divided by 100
*      year % 4 == 0 && year % 100 !=0 || year % 400 == 0
* Condition 2: can be divided by 400
* */
//Give the user a prompt
System.out.println("Please enter the year you want to judge:");
//Accept data from console
Integer year = new Scanner(System.in).nextInt();
if((year % 4 == 0 && year % 100 !=0 )|| year % 400 == 0) {
System.out.println(year+"It's ruinian");
}else {
System.out.println(year+"It's a normal year");
}

}

}

```

### 3.2 self increasing and self decreasing

```        int a = 1;
System.out.println(a++);//1. After the symbol, use it first, and then add it automatically
System.out.println(a);//2. Increase to 2 after use

int b = 1;
System.out.println(++b);//2. When the symbol comes first, it is added automatically before use

int c = 1;
System.out.println(--c);//0, before the symbol, subtract first and then print

int d = 1;
System.out.println(d--);//1. After the symbol, print first and then subtract automatically
System.out.println(d);//0. It will be reduced after use

/**The previous code will have an impact on the later code*/
System.out.println(c);//The initial value of c is 0
/**Only the self increasing and self decreasing operator can change the value of the variable itself
* Ordinary four operations can only change the value of the formula itself*/
System.out.println(--c-c-c--);//-1-(-1)-(-1)=1
//System.out.println(c);//-2

```

### 3.3 compare the size of two numbers

Requirement: accept the two numbers a and B entered by the user, and compare the size of the two numbers

```package cn.tedu.basic;

import java.util.Scanner;

/**Requirement: receive two integers input by the user, and compare and output the maximum value of the two numbers*/
public class TestMaxNum {
public static void main(String[] args) {
//1. Prompt the user for input
System.out.println("Please enter the first integer you want to compare:");
//2. Receive the integer entered by the user and give the value to variable a to save
int a = new Scanner(System.in).nextInt();
System.out.println("Please enter the second integer you want to compare:");
int b = new Scanner(System.in).nextInt();

//3. Compare the received two numbers and use the ternary operator
/**1 ? 2 : 3
* 1 Is an expression. If the result of 1 is true, the result takes position 2. If the result of 1 is false, the result takes position 3
* */
//4. Define the variable max to save the maximum value of the comparison
int max = a > b ? a : b;

//5. Print maximum value
System.out.println("The maximum of the two numbers is:"+max);
}
}

```

### 3.4 compare the size of three numbers

Demand: how to determine the maximum value of three numbers a, B and C?

```package cn.tedu;

import java.util.Scanner;

//Compare the maximum of three numbers a, B and C
public class Demo02 {
public static void main(String[] args) {
//Define three variables of type int
System.out.println("Please enter the first decimal a: ");
Double a = new Scanner(System.in).nextDouble();
System.out.println("Please enter the second decimal b: ");
Double b = new Scanner(System.in).nextDouble();
System.out.println("Please enter the third decimal c: ");
Double c = new Scanner(System.in).nextDouble();

Double max = a>b? (a>c? a:c):(b>c? b:c);
System.out.println("The maximum value of three decimals is:"+max);

}
}
```

## 4. Expansion

### 4.1 self increasing and self decreasing operators of arithmetic operators

A is the operand, + + is the self increasing operator, and - is the self decreasing operator. The self increasing and self decreasing operators can be placed in front of or behind the variable, such as a + +, + + A, a –, -- A, etc.
Auto increment (+ +): increase the value of the variable by 1
It is divided into prefix formula (such as + + a) and suffix formula (such as a + +). Prefix formula is to add 1 before use; The suffix is to use first and then add 1,
Self subtraction (–): subtracts the value of a variable by 1
Divide prefix (e.g. – a) and suffix (e.g. a –). The prefix formula is to subtract 1 before use; The suffix is used first and then minus 1.

### 4.2 logical operators

• Logical operators connect two relational expressions or boolean variables to solve the combination judgment problem of multiple relational expressions

Note that the operation result returned by the logical operator is Boolean

• Generally, in actual development, 0 represents false and 1 represents true
• Relationship with: express and

& Single and: 1 & 2. If the result wants to be true, both 1 and 2 must be true
&& Double and (short circuit and): 1 & & 2. When 1 is false, 2 will be short circuited to improve the efficiency of the program

• Or: a relationship that represents or

| Single or: 1 | 2. If the result wants to be true, only one of 1 and 2 is required to be true
|| Double or (short circuit or): 1 | 2. When 1 is true, 2 will be short circuited to improve program efficiency

### 4.3 priority control

When an expression contains multiple operators, the priority of the operator needs to be considered. Operators with high priority participate in the operation first, and operators with low priority participate in the operation later. In the actual development, there is no need to memorize the priority of operators, and do not deliberately use the priority of operators. For places where the priority is not clear, use parentheses to assist in priority management.

Tags: html css Java

Posted by ollie007 on Wed, 22 Sep 2021 20:21:50 +0530