1. operator
Basic concepts of 1.1 operators
int num = 10+5;
- = And + are operators
- The contents of left and right variables of operators are called operands. One operand on both sides of an operator is called a single unit or two units: binocular or binary operators.
- 10 + 5 is called an expression, and there is an operation at the end.
- int num=10+5; a statement; the end is called a statement
Classification of 1.2 Operators
- Arithmetic Operator + - */%+ -
- Relational Operator > < >= <=!==== instanceOf
- Logical Operator & |!^&&||
- Assignment operator=
- Extension operator += - = *=%= > >= <= ^=!=
- Bit Operator & | ^ > > < > > > > > > > >
- Conditional operator:?
operator | operation | Example | Result |
---|---|---|---|
+ | Plus sign | +3 | 3 |
- | Minus sign | b=4; -b | -4 |
+ | plus | 5+5 | 10 |
- | reduce | 6-4 | |
* | ride | 3*4 | |
/ | except | 5/5 | |
% | Mould (Remainder) | 7%5 | |
++ | Self-increasing (before): First calculate, then take value | a=2;b=++a | a=3;b=3 |
++ | Self-increment (after) first takes value and then calculates | a=2;b=a++ | a=3;b=2 |
– | Auto-subtraction (before): First calculate and then take value | a=2;b=–a | a=1;b=1 |
– | Self-subtracting (after): First take value, then calculate | a=2;b=a– | a=1;b=2 |
+ | String connection | "He"+"llo" | "Hello" |
1.2.1 assignment operator
= Store the results on the right of the assignment operator to the variables on the left
int num = 10;
- Operating from right to left
1.2.2 arithmetic operator
+ number
public class Operator01{ public static void main(String[] args){ int num = +5; System.out.println(num); num = 5+3; System.out.println(num); //Connection symbol System.out.println("1+1="+1+1); System.out.println("1+1="+(1+1)); } }
Matters needing attention
- Positive sign, written before value
- The addition operation guarantees that the left and right sides of + are a specific value.
- Connector, +has an operand on both sides of the string type, at this time + is a connector, its operation rules will be around the operand as a string, and then splicing (the result of splicing or string).
- No.
public class Operator01{ public static void main(String[] args){ int num = -5; System.out.println(num); num = 5-4; System.out.println(num); } }
- % /%
public class Operator02{ public static void main(String[] args){ int num = 3*3; System.out.println(num); num = num/3; System.out.println(num); num = num%2;//3%2 System.out.println(num); } }
+ + and -
- ++ and --- are both monocular operators
- Linkages and Differences between + + and -
- contact
- ++ operators are self-increasing on the original basis
- Operators are self-decreasing on the original basis
- When + + and --- are separate statements, ++ and --- are monotonic operators, but there is no difference between the former and the latter, they are both self-increasing 1 and self-decreasing 1.
- Distinguish (when not a single statement)
- Num++=> num++ Finally, there is a result that num itself has a result; the value of num++ expression and num are related to the value of num++ expression= the value of num.
- + + num is not a single statement, add before, add first and then calculate.
- When num++ is not a single statement, add it, first calculate it and then add it.
- num is not a single statement: subtract first, subtract first and then operate.
- num --- When not a single statement, add it, operate it first and then reduce it.
- contact
public class Operator03{ public static void main(String[] args){ int num = 10; //Using + + Self-Increasing Operator--Self-Decreasing Operator --num; System.out.println(num); //Let num reassign 10 num = 10; //Assign num to a new variable result after self-increment or self-decrement int result = ++num; //Print out num and result values System.out.println("num:"+num+"result"+result); int k = 5; int j = 6; result = k++ + ++k -k++ + j-- - ++j + j-- + ++k; System.out.println("k:"+k+",j:"+j+",result:"+result); } }
1.2.3 Extended Operator
Essentially, it's a combination of arithmetic operators and bit operators plus assignment operators. += - = /= etc.
operator | operation | Example | Result |
---|---|---|---|
== | Equal | 4==3 | false |
!= | Not equal to | 4!=3 | true |
< | less than | 4<3 | false |
> | greater than | 4>3 | true |
<= | Less than or equal to | 4<=3 | false |
>= | Greater than or equal to | 4>=3 | true |
instanceOf | Detecting whether it is an object of a class | "Hello" instanceof String | true |
public class Operator04{ public static void main(String[] args){ // Calculate the sum of the scores of two students int zhangScore = 44; int liScore = 33; //Summation int sum = 0; sum = sum+ zhangScore;//sum +=zhangScore;// sum = sum+ zhangScore; sum = sum+ liScore;//um +=liScore;//sum /= zhangScore; System.out.println(sum); int num = 1000; byte n = 0; n += num; System.out.println(n); } }
Analysis of Advantages and Disadvantages of Extended Operators
- Advantage
- Increased compilation speed
- Increased writing speed
- Automatic forced type conversion
- shortcoming
- Not conducive to reading
1.2.4 relational operator
public class Operator05{ public static void main(String[] args){ boolean flag = 4>10; System.out.println(flag); flag = 4!=5; System.out.println(flag); flag = 1*31+22>33; System.out.println(flag); // Error in Operator Type of Binary Operator'>' //System.out.println("hello">"hello"); //System.out.println(true>true); int num = 1+true; } }
Matters needing attention:
- The final result is a boolean type of value true and false
- !===== You can compare both basic data types and reference data types.
- < > >= <== Only basic data types can be compared.
1.2.5 Logic Operator
a | b | a&b | a | b | a || b | !a | a^b |
---|---|---|---|---|---|---|
true | true | true | true | true | false | false |
true | false | false | false | true | false | true |
false | true | false | false | true | true | true |
false | false | false | false | false | true | false |
- Logical operators are used to connect Boolean expressions. They can not be written as 3 < x < 63 < x < 63 < x < 63 < x < 6, but should be written as x > 3 & x < 6 in Java.
- The Difference between "Harmony" and "Harmony"
- Single-time, the left side of the operation regardless of true or false right;
- If the left side is true, the right side is involved in the operation, and if the left side is false, the right side is not involved in the operation.
- The difference between'|','|'and'|' is the same,'|'means that when the left side is true, the right side does not participate in the operation.
- The difference between XOR (^) and OR (|) is that when both sides are true, the result is false.