preface
C: A computer, as the name suggests, is used for computing data. The programs that run on the computer are also used to calculate and process data, and every programming language has a number of commonly used operators to support our data processing and calculation.
And they are not difficult at all if we have a good mathematical background.
A series of reading
-
The Java syntax | Eclipse person”
-
The Java syntax | programs that something”
-
The Java syntax | variables and constants,
-
The Java syntax | common operators”
The assignment operator
The first is the = sign, which in Java, as we saw last time, no longer means equality, but assignment. Every time you encounter it, you have to brush your math brain: “It’s not an equality judgment, it’s an assignment sign, which assigns what’s on the right of the equality sign to the variable on the left.”
Case requirement: Student Wang Hao’s Java score is 80, student Zhang Meng’s Java score is the same as Wang Hao’s, output Zhang Meng’s score.
public class Demo {
public static void main(String[] args) {
// Wang Hao performance
int wScore = 80;
// Zhang Meng grade
int zScore;
// Assign: Assign the value on the right side of the equals sign to the value on the left
zScore = wScore;
System.out.println(Zhang Meng's results are:+ zScore); }}Copy the code
In almost all programming languages, the = sign indicates assignment, or both assignment and equality.
Let’s do another example to feel the assignment notation.
Case requirement: exchange num1 and num2 values.
Num1 and num2 can be swapped using the third variable as an intermediary.
Teacher Zha said: “When we want to exchange the liquid in a bottle of Sprite and a bottle of coke, we cannot directly exchange them. We need to use an empty bottle as a transfer.
public class Demo {
public static void main(String[] args) {
int num1 = 8;
int num2 = 9;
// This conversion method is not feasible, similar to Sprite and Coke directly on the mouth
// num1 = num2; Num1 is 9
// num2 = num1; Num2 is 9
// Define an intermediate variable to implement the swap
int temp;
temp = num1; / / temp is 8
num1 = num2; / / num1 is 9
num2 = temp; / / num2 is 8}}Copy the code
Arithmetic operator
Next to assignment symbols, arithmetic operators are probably the most familiar:
+
Addition operator-
Subtraction operator*
Multiplication operator/
The division operator%
Modulo operator (remainder)++
Increment operator--
Decrement operator
arithmetic
These operators, the first five of which are pretty simple, look at the following example:
int num1 = 11;
int num2 = 2;
System.out.println(num1 + num2); / / 13
System.out.println(num1 - num2); / / 9
System.out.println(num1 * num2); / / 22
System.out.println(num1 / num2); / / 5
System.out.println(num1 % num2); / / 1
Copy the code
You must be wondering why 11/2 = 5. Not equal to 5.5, right? In fact, this involves the problem of data types. In Java, when two numbers participate in the calculation, the type with a large range of values will be the type of the final calculation result.
Since there are two int variables at the top, the result is also int. If you want more precise results, you can rewrite the output statement as num1 * 1.0 / num2. In this case, if one of the parties has changed to a larger value range, the type of the result will also change to a larger value range.
Autoincrement and autodecrement
But you might be wondering about the last two arithmetic symbols, ++ and — what exactly are they for?
// ++ and -- are both pre and post forms
int num1 = 10;
// In the following example, take the ++ symbol as an example.
// 1. If written as a separate statement, the preceding ++ and the following ++ both indicate the increment of 1
Num1 = num1 + 1; (num1 = 10 +1) Add the value of num1 and assign it to num1 so that num1 is 11
// num1 ++;
// ++ num1;
// 2. However, if ++ is used in complex situations, there is a difference between pre and post
// System.out.println(++ num1); / / 11
System.out.println(num1 ++); / / 10
System.out.println(num1); / / 11
Copy the code
Teacher Zha said: the reason for this kind of result, the current simple understanding is that ++ in front means that the value is increased first and then used, and ++ after means that the value is used first and then increased.
Namely: System. Out. Println (+ + num1); In, num1 is incremented to 11, and then output.
System.out.println(num1 ++); Num1 = 11; num1 = 10; num1 = 11; num1 = 11;
Let’s take a look at the code below and try to read what it means to see if the results are what you expect.
int num1 = 5;
int num2 = 2;
int a = num1 % num2;
int b = num1 / num2;
System.out.println(num1 + "%" + num2 + "=" + a); // 5%2 = 1
System.out.println(num1 + "/" + num2 + "=" + b); // 5/2 = 2
num1 ++;
num2 --;
System.out.println("num1 = " + num1); // num1 = 6
System.out.println("num2 = " + num2); // num2 = 1
Copy the code
Teacher Zha said: learn to read code, it is really important, whether you write it or others write it, if you do not understand them, you can not understand what it wants to do, do not be afraid! Try it!
The secret of the char type
Added: ++ and — Problems with char encounters:
// If a character performs a numerical calculation, it is automatically converted to the corresponding code table value and then performs the calculation
char alp1 = 'a';
char alp2 = 'A';
System.out.println(alp1 + 1); / / 98
System.out.println(alp2 + 1); / / 67
Copy the code
Char can be converted to an int by using ASCII, DBCS and other code tables. For example, lowercase A is 97 and uppercase A is 65.
ASCII (American Standard Code for Information Interchange) is a computer coding system based on the Latin alphabet, which is mainly used for displaying modern English and other Western European languages. It is the most common standard for information exchange and is equivalent to the international standard ISO/IEC 646. ASCII was first published as a canonical standard in 1967, and was last updated in 1986, with 128 characters defined so far. [1]
To address some compatibility between Chinese, Japanese, and Korean hieroglyphic characters and ASCII, double-byte character sets (DBCS) emerged. DBCS starts with code 256, and just like ASCII, the first 128 codes are ASCII. However, some of the higher 128 codes always follow the second byte. Together, these two bytes (called the first byte and the following byte) define a character, usually a complex hieroglyphic. [2]
// If ++ or -- is executed, the char type is first converted to an integer and then converted to a char after the calculation
char alp1 = 'a';
alp1 ++; // alp1 = alp1 + 1;
System.out.println(alp1); // b
Copy the code
The compound assignment operator
After learning about both assignment and arithmetic operators, Cha will introduce you to a special type of operator in Java: the compound assignment operator, which is a special way of combining assignment and arithmetic operators.
+ =
- =
* =
/ =
% =
Although they are strange, they are really easy to understand. Take a look at the example below:
int num1 = 10;
num1 -= 2; // equivalent to num1 = num1-2;
System.out.println(num1); // The result is 8
Copy the code
Relational operator
There are also relational operators that can be used for judgment in Java, and we will use many of them in the future.
For example, error messages are generated when the user name length is less than 5. These are the scenarios for relational operators.
The relational operator evaluates to a Boolean value, either true or false.
The operator | meaning | The sample | The results of |
---|---|---|---|
= = | Is equal to the | 5 = = 6 | false |
! = | Is not equal to | 5 != 6 | true |
> | Is greater than | 5 > 6 | false |
< | Less than | 5 < 6 | true |
> = | Greater than or equal to | 5 > = 6 | false |
< = | Less than or equal to | 5 < = 6 | true |
// The following code is enclosed in () purely to keep == and = separate for easier reading
boolean result = (5= =2);
System.out.println(result); // false
// Not equal in Java! = said
boolean result1 = (5! =2);
System.out.println(result1); // true
Copy the code
Teacher Zha said: the way to remember is to write more comments on the corresponding code, the = sign represents the assignment operator == represents the equality relationship.
Logical operator
In daily life, there are many situations where not a relational operator can do a good job. For example, IF I want to represent a certain grade range of 70 to 80, I can’t use 70 < x < 80 in Java. In this case, I need a logical operator.
By logical operators, we mean and or these in our lives. 70 < x < 80 means that x is greater than 70 and x is less than 80, which can be written as x > 70&&x < 80 using the logical operator.
The operator | meaning | algorithm |
---|---|---|
&& | A short circuit 与 | If both operands are true, the result is true |
|| | A short circuit 或 | If one of the operands is true, the result is true |
! | non | The operand is true and the result is false; If the operand is false, the result is true |
// short-circuit and: && is understood to be true and both conditions are true. True && true == true;
/ / short circuit or: | | understood as or as long as there is a condition is true, the result is true true | | false = = true;
/ / a:! Understand to take inverse! true == false; ! false == true;
boolean flag1 = (2= =3); // false
boolean flag2 = (2= =2); // true
boolean flag3 = flag1 && flag2;
boolean flag4 = flag1 || flag2;
System.out.println(flag3); // false
System.out.println(flag4); // true
Copy the code
Magical short circuit function
Actually besides && and single & also can express the same meaning, | | and | can also express the same meaning.
But we generally recommended to use && and | |, reason is that they have short circuit function.
// ====== Non-short circuit ======
int num1 = 7;
// Whether the first expression is true or not, subsequent expressions are executed
boolean flag2 = false& (++ num1 ! =8);
System.out.println(flag2); // false
// num1 is executed, so the result is 8
System.out.println(num1); / / 8
Copy the code
// ====== Short circuit ======
int num1 = 7;
// Since the first expression is false, it already determines that FLAG2 is false, so the && expression will not be executed, resulting in a short circuit
boolean flag2 = false && (++ num1 == 8);
System.out.println(flag2); // false
// ++ num1 is not executed, so the value is left unchanged
System.out.println(num1); / / 7
Copy the code
Teacher Zha said: there is no doubt that the short circuit symbol can save the execution efficiency of the program.
Ternary operator
There is also a special type of operator supported in Java, the ternary operator, also known as the ternary operator.
// Ternary operator (ternary operator)
/ / conditions? Expression 1: expression 2
// If the condition is true, expression 1 is executed, otherwise expression 2 is executed
int min = (5 < 7)?5 : 7;
System.out.println(min); / / 5
int max = (10 < 7)?7 : 10;
System.out.println(max); / / 10
Copy the code
Teacher cha said: the late use is quite a lot, with good it can effectively optimize some code structure, simplify the code.
Operator priority
As for the precedence of arithmetic symbols, Mr Cha thinks there is no need to remember, just remember: you want the first person to run, put a parenthesis on it. And in fact, we’ve learned from years of math that parentheses make reading easier.
- The unary operators include
!
、++
,--
, has a high priority - The lowest priority assignment operator
- Can be achieved by
(a)
Control the order in which expressions are evaluated,(a)
Highest priority - Only assignment operators, ternary operators, and unary operators are associative from right to left
- Arithmetic operators > relational operators > logical operators
reference
[1] Mo Shaoqiang, Chen Shanguo. Computer Application Basic Course: China Railway Publishing House, 2012:12-13
[2] Zhong Xiaoli, XIE Minmin, Li Yongning. Research on character encoding and Unicode encoding [J]. Business Management, 2010(20):364-364.
Afterword.
Ok, the commonly used operator introduction is over here, this homework please search wechat attention: check the teacher’s handout, and then reply to the commonly used operator homework.
We have a basic understanding of these symbols, can you use? Does your math foundation empower you? Computer learning is not only to deal with English, mathematics is more important, but fortunately, the update of the teacher is small white text, the basic requirements of mathematics is not too much.
Teacher Zha said: For the learning of technology, teacher Zha has always followed the following steps: With a simple demo to let it run first, and then learn it the most commonly used API and configuration can let yourself up, finally, on the basis of proficiency in the spare time reading the source to try to make myself to be able to see clearly its running mechanism, part of the cause of the problem, at the same time, draw lessons from these technology to enhance your own code level.
So in the teacher’s article, the early basic are small white, only interspersed with a very small amount of source research. Of course, such as the small white update, you still like, the late will not regularly dedicated to part of the technology of the source code analysis.