Getting started with Java: operators,
Operators are classified into operators and operators.
Operation: Is an action. It obtains a new quantity through a combination of known quantities.
Character: In ancient times, the symbol is the secret pattern of the communicating person and the god, so the character cannot be randomly painted. Therefore, there is a so-called "the symbol is unknown, and it will not make the ghost laugh; if it is known,. There are hundreds of ways to draw symbols. Some of them need to worry about the gods, and some need to fight and read the magic words ......
Well, I know the origins of the operators, so if the program is well written, it seems that the operators are used badly!
======================================================= =====
Of course, the above are all nonsense. What exactly is the operator in Java?
In fact, it is similar to the calculation operator in mathematics. Please Look at the table below according to the calculation priority:
Sequence |
Operator |
1 |
Parentheses, such as () and [] |
2 |
Unary operators, such as-, ++,--, and! |
3 |
Arithmetic Operators, such as *,/, %, +, and- |
4 |
Relational operators, such as >,>=, <=, ==, and! = |
5 |
Logical operators, such as &, | |
6 |
Conditional operators and value assignment operators, such? :, =, * =,/=, + =, And-= |
Most of the symbols are okay if you look at them? If you have any questions, please leave a message. Haha
Next, I will briefly introduce a few symbols not recognized by beginners. If you have a party, please pass by quickly...
Unary operator:
++: The value of the variable itself is basically + 1.
1234567 |
public static void main(String[] args){ int i = 5 ; int j = i++; // ++ Is followed, so run j = I first, then I ++. After the execution, I = 6, j = 5; int k = ++j; // ++ Is in the front, so execute ++ j first, then k = j, and then j = 6, k = 6; System.out.println( "J value :" +j); System.out.println( "K value :" +k); } |
--: The principle is the same as that of ++, but it is changed to-1.
Arithmetic Operators:
*: Indicates multiplication.
123 |
int i = 12 ; int j = 5 ; int k = i*j; // The value of k is 60. |
/: Indicates division.
123 |
int i = 12 ; int j = 5 ; int k = i/j; // The value of k is 2 |
Note: Because I/j is an integer, the calculation result does not include the decimal part. Even if k is defined as double, the result is 2.0.
%: Returns the remainder. Let me know the remainder.
123 |
int i = 13 ; int j = 5 ; int k = i%j; // The value of k is 3. |
Relational operators:
! =: Not equal. Do not write it as "<> ".
=: Equal. Do not use only one "= ".
Logical operators:
In fact, "!" A logical operator that returns the opposite result.
You can understand the logical operators through an example.
I remember when I first went to college, a roommate vowed to "find a rich and beautiful girl as a girlfriend". But two years have passed and I have never been satisfied with it, then he lowered his request to "find a rich girl | pretty girl as a girlfriend", and two years later he still did not find satisfaction, finally, he lowered his requirements again. "Find a girlfriend! ".
Haha, read it !!!
Assignment operator:
=: Put the Operation Result of the right expression in the variable on the left, so the Left can only be a variable, not an expression.
+ =: Add the data on the right to the left variable, and assign the result to the variable on the left.
12 |
int i = 13 ; i += 3 ; // The result of I is 16 |
Other (-=, * =, etc.) functions are the same.
Conditional operators:
? : This is a three-object operator (there should be three expressions ).
123 |
int i = 13 ; // Change the I value to 5 and then check what the str result is. // I> 10, the str value is "yes", otherwise it is "no" String str = i > 10 ? "yes" : "no" ; |
Although the operators seem to have so many symbols, they are often used in the coding process, and they are also relatively simple to use, so you don't have to memorize them.
OK. Here is the content of this article.
"Software thinking" blog address:51CTO,BlogInterested friends can visit other related blog posts.